Re: String to Integer Conversion

Steve Johnson <[email protected]> Fri, 21 Jun 2002 13:17:53 -0600
Newsgroups gmane.comp.windows.devel.dotnet.general
Organization 3t Systems
Message-ID <[email protected]>
> I have a small program which has 2 variables(1 string
> variable and other an integer variable). I have to
> convert the value in the string variable to an integer
> and store it in the integer variable. Before
> converting it into an integer I want to check whether
> the string varibale contains valid numbers if so, I
> would go ahead and convert otherwise I have to store
> 0.

This should do the trick:

try
{
    val = Int32.Parse(str1)
}
catch(FormatException)
{
    val = 0;
}
catch(ArgumentNullException)
{
    val = 0;
}



--
Steve Johnson
3t Systems

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.