Saturday, May 22, 2010

C# value convertion?

In C#: Can I convert a string to an int?

C# value convertion?
int intNumber = Convert.ToInt32(strNumber);





A safer way is to use int.TryParse, but its a bit more code.





http://msdn2.microsoft.com/en-us/library...
Reply:int myInt;


String myIntString = "1234";





if (int.TryParse(myIntString, myInt))


{


// Parse was successful


// Do something with myInt


// If myIntString contained something other then and int


// "abc" for example this block would never be run


}


No comments:

Post a Comment