Getting a std string into a String^

romeoagogo

Limp Gawd
Joined
Aug 25, 2004
Messages
159
Hi, I am trying to set the text of a textbox in VC++ but textboxes only take String^ not std::string. I have a function to change a String^ into a std::string but not vice versa. According to this you can simply do: String^ s = somestring.c_str(). Here is my code followed by the error.

Code:
String ^rd;
std::string read;

read = "abcd";
rd = read.c_str();

and the error:

Code:
c:\documents and settings\ac251404\my documents\visual studio 2005\projects\project1\project1\Form1.h(590) : error C2440: '=' : cannot convert from 'const char *' to 'System::String ^'
        No user-defined-conversion operator available, or
        Cannot convert an unmanaged type to a managed type
 
"Whidbey" mentioned in that link is VS2005. Anson Tsao gave an answer that applies:

Code:
	String ^rd;
	std::string read;

	read = "abcd";
	rd = gcnew String(read.c_str());
 
Back
Top