Saturday, March 6, 2010

How to check user input in C# .NET

How to parse user input using C# (.NET)
Converting the input, for example, converting integer can be done as shown below
public static void TryParse_Examples()
{
bool bConversion;
int result;
// Failure //
bConversion = int.TryParse(null, out result);
if (bConversion == false)
{
MessageBox.Show ("Error Occurred!");
}
// Failure //
bConversion = int.TryParse("s1", out result);
if (bConversion == false)
{
MessageBox.Show("Error Occurred!");
}
// Success //
bConversion = int.TryParse("100", out result);
if (bConversion == false)
{
MessageBox.Show("Error Occurred!");
}
}

No comments:

Post a Comment