"steven" <s_mumby@hotmail.comwrote in message
news:80102f62-22db-49d9-8e0e-678e0f383d12@l43g2000hsh.googlegroups.com...
Quote:
Anybody know why there's no TryParse method for a string type? I can
do the following when trying to convert an object to int:
>
object oTest = 1;
int iValue;
int.TryParse(oTest, iValue);
>
But there's no equivalent for string:
>
object oTest = 1;
string sValue;
string.TryParse(oTest, sValue); // method TryParse doesn't
exist so this won't compile //
There's no String.Parse() either. That's because the method Parse, by
convention, _parses_ a string to produce a value of a different type. The
conventional definition of "parse" is that you parse strings, not integers.
To get a string representation of any .NET object/value, use its ToString()
method. At the very least, there's always Object.ToString(), and types such
as Int32 or DateTime usually define additional overloads that provide extra
formatting options. Also, if you want to convert a value of an unknown type
to string in a culture-invariant manner, use Convert.ToString().