Wednesday, February 28, 2007

int to String under Managed C++

I have been doing a little managed C++ at work the last couple of days. Did you know you could do this:

System::String^ str = gcnew System::String("");
str += 5;
System::Console::WriteLine(str); // prints: 5

or simply:

System::String^ str = "";
str += 5;
System::Console::WriteLine(str); // prints: 5

or even:

System::String^ str;
str += 5;
System::Console::WriteLine(str); // prints: 5

The 5 is converted to a String for you. After working with C++ for so many years without a simple way to convert an int to a string this is nice. Might not seem like that big a deal, but these little things add up.

5 comments:

Anonymous said...

Just started C++ today, and already I owe you one

Anonymous said...

Nice and concise, best solution I've seen so far.

Sanket said...

Totally good!

Anonymous said...

thx a bunch man! been looking for a simple solution like this for so long

Anonymous said...

Thank u that was simple and good.