Windows Phone Developers

Wednesday, December 23, 2009

How to check user input in C# (.NET) / VB.NET

How to parse user input using C# (.NET)

Many times we do not get the anticipated input from the user (either as direct entry / through some input files). Converting the input, for example, to say 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!");

}

}

Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

1 comment:

  1. Hi,

    Thank you for nice post. Please keep posting such a nice stuff. I would like to introduce another good C# blog, Have a look.

    http://CSharpTalk.com
    Sonam

    ReplyDelete

kbAlertz.com :: Visual Studio 2005

kbAlertz.com :: Visual Studio 2008

kbAlertz.com :: Visual Basic 2005