1 Replies - 1161 Views - Last Post: 27 March 2008 - 02:49 PM Rate Topic: -----

#1 Mila  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 34
  • View blog
  • Posts: 193
  • Joined: 28-October 06

Converting std::string to System::String

Posted 27 March 2008 - 02:32 PM

I tried googling, but it only served to confuse me further. So I figured I'd ask here.

I'm creating a simple .NET application. At one point I have information in a std::string and I need to get a textbox to show it.

Originally, I had coded:
questionBox->Text = question;


... where question is a previously defined string.

That gave me a compilation error (error C2664: 'void System::Windows::Forms::Control::Text::set(System::String ^)' : cannot convert parameter 1 from 'std::string' to 'System::String ^'). Then I tried to convert:

questionBox->Text = Convert::ToString(question);



This time, the compilation error is:

Quote

error C2665: 'System::Convert::ToString' : none of the 37 overloads could convert all the argument types
error C2665: 'System::Convert::ToString' : none of the 37 overloads could convert all the argument types
c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll: could be 'System::String ^System::Convert::ToString(System::Object ^)'
c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll: or 'System::String ^System::Convert::ToString(bool)'
c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll: or 'System::String ^System::Convert::ToString(wchar_t)'
c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll: or 'System::String ^System::Convert::ToString(signed char)'
c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll: or 'System::String ^System::Convert::ToString(unsigned char)'
c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll: or 'System::String ^System::Convert::ToString(short)'
c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll: or 'System::String ^System::Convert::ToString(unsigned short)'
c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll: or 'System::String ^System::Convert::ToString(int)'
c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll: or 'System::String ^System::Convert::ToString(unsigned int)'
c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll: or 'System::String ^System::Convert::ToString(__int64)'
c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll: or 'System::String ^System::Convert::ToString(unsigned __int64)'
c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll: or 'System::String ^System::Convert::ToString(float)'
c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll: or 'System::String ^System::Convert::ToString(double)'
c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll: or 'System::String ^System::Convert::ToString(System::Decimal)'
c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll: or 'System::String ^System::Convert::ToString(System::DateTime)'
c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll: or 'System::String ^System::Convert::ToString(System::String ^)'
while trying to match the argument list '(std::string)'


From my googling, I now understand a little more the differences between std::strings and System::Strings, but still can't, for the life of me, figure out how to convert from one to the other.

Any help?

Is This A Good Question/Topic? 0
  • +

Replies To: Converting std::string to System::String

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Converting std::string to System::String

Posted 27 March 2008 - 02:49 PM

Yeah it is a bit tricky, here you go...

// Create our std::string
std::string ourString = "Test text";

// Convert it to a managed string by constructing a managed String object using our std::string converted to a c_string
String^ managedString = gcnew String(ourString.c_str());

// Now we have our managed string for the textbox. 
textBox1->Text = managedString;



I am not sure why you have an unmanaged string to begin with, but if you are going VC++.NET you should be trying to use all managed code.

Hope this helps!

"At DIC we be managed code ninjas, even though some say we can't be managed." :snap:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1