Windows Phone Developers

Showing posts with label Int.TryParse in C#. Show all posts
Showing posts with label Int.TryParse in C#. Show all posts

Saturday, March 17, 2012

C# IsDate Function / How to check valid date using C# function

IsDate Function in .NET

If you want to check if the given entry (string) is a valid date you could have used Microsoft.VisualBasic.IsDate Function in .NET. This function is available in VB.NET. For C# you can have a similar function

Here is a hint how to create that

private void IsDateExample()
        {
            //string s1 = DateTime.Now.ToString();
            bool bSuccess = false;
            DateTime d1;
            s1 = "17-Mar-20112";
            bSuccess = DateTime.TryParse(s1, out d1);
            if (bSuccess == true)
            {
                Console.WriteLine(d1.ToString());
            
            }
            else
            {
                Console.WriteLine("Not a date at all");
            }


 

        }




The function uses DateTime.TryParse to validate the given string 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

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