public knowledge blog_load(sender Virendra)

                                                                                                                          Nothing shocks me, I am a Software Engineer.

Parse and TryParse

Posted by Virendra Dugar on January 30, 2009

Parse method is used to parse any value to specified data type.

For example
string test = “42”;
int32 Result;
Result = Int32.Parse(test);

This will work fine but what if when you are not sure about the value of string variable test. As for example if test=”abc” and if you try to use above method, .NET will throw an exception as you are trying to convert string data to integer.

TryParse is a good method if the string you are converting to an interger is not always numeric.

if(!Int32.TryParse(test,out iResult))
//do something

TryParse method returns a boolean to denote whether the conversion has been successfull or not, and returns the converted value through an out parameter.

**declare variable iResult of Int32.



One Response to “Parse and TryParse”

  1. deepak said

    In window application i have my first form that is login form allrite. whenever a user run the program if login is successful then it will show me homepage. now the problem is this if login is successful it will show me the homepage but the login page should automatically close after successful login???

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: