Software Development
Blogs and Discussion
developer.*
Books Articles Blogs Subscribe d.* Gear About Home

C# and VB.NET IsNumeric() and IsDate() Functions

As you have probably already realized if you found this page through a search, for some reason they left IsNumeric() and IsDate() functions out of .NET. I end up needing these is almost every non-trivial project.

Update: I'm a little slow on the uptake, but a week after writing this post, I noticed that VB.NET now has intrinsic IsNumeric() and IsDate() functions; check the Microsoft.VisualBasic namespace. Keep reading, though, if you need these for C# or for VB.NET 1.x.

Your mileage may vary depending on your performance needs; if you're calling these thousands of times in a loop, then be sure to test them against alternate techniques. Here are C# and VB.NET versions of the functions I use:

C# IsNumeric() Function

Double.TryParse() is available in .NET 1.1 and 2.0, so this function works with either.

public static bool IsNumeric(string anyString)
{
if (anyString == null)
{
anyString = "";
}
if (anyString.Length > 0)
{
double dummyOut = new double();
System.Globalization.CultureInfo cultureInfo =
new System.Globalization.CultureInfo("en-US", true);

return Double.TryParse(anyString, System.Globalization.NumberStyles.Any,
cultureInfo.NumberFormat, out dummyOut);
}
else
{
return false;
}
}

C# 1.1 IsDate() Function

Since we're assuming a strongly typed language, if the value already exists in a DateTime variable, then we can assume that it is a valid value. So we start with the assumption that the user has the value in a string or something easily converted to a string. This routine is .NET 1.1 compatible since we're not using DateTime.TryParse().

public static bool IsDate(string anyString)
{
if (anyString == null)
{
anyString = "";
}
if (anyString.Length > 0)
{
DateTime dummyDate = null;
try
{
dummydate = DateTime.Parse(anyString);
}
catch
{
return false;
}
return true;
}
else
{
return false;
}
}

An alternate variation removes some of the cohesion from the routine, but you could also make resultDate variable in this an 'out' argument and make it that much easier for the caller to get a DateTime value once it's been confirmed that the value is indeed a date; (this is basically how the DateTime.TryParse() function added in .NET 2.0 works, anyway, so it's not inconsistent). Here is my version of this alternate syntax. In C# it's easy enough to offer overloaded versions of both (also .NET 1.1 compatible):

public static bool IsDate(string anyString, out DateTime resultDate)
{
bool isDate = true;

if (anyString == null)
{
anyString = "";
}
try
{
resultDate = DateTime.Parse(anyString);
}
catch
{
resultDate = DateTime.MinValue;
isDate = false;
}

return isDate;
}

VB.NET IsNumeric() Function

Double.TryParse() is available in .NET 1.1 and 2.0, so this function works with either.

Public Shared Function IsNumeric(ByVal anyString As String) As Boolean
If anyString Is Nothing Then
anyString = ""
End If
If anyString.Length > 0 Then
Dim dummyOut As Double = New Double()
Dim cultureInfo As System.Globalization.CultureInfo = _
New System.Globalization.CultureInfo("en-US", True)

Return Double.TryParse(anyString, System.Globalization.NumberStyles.Any, _
cultureInfo.NumberFormat, dummyOut)
Else
Return False
End If
End Function

VB.NET 2.0 IsDate() Function

This code will only work in .NET 2.0 and above, since it depends on DateTime.TryParse(), which was not added until 2.0. If you need a VB.NET 1.1 IsDate() function you can rewrite the C# 1.1 version above without too much trouble.

Public Shared Function IsDate(ByVal anyString As String) As Boolean
If anyString Is Nothing Then
anyString = ""
End If
If anyString.Length > 0 Then
Dim dummyOut As Date = Nothing
Return DateTime.TryParse(anyString, dummyOut)
Else
Return False
End If
End Function
Categories:  | |

CORRECTION to your

CORRECTION to your code:

DateTime dummyDate = null;

this cannot be as DateTime is non-nullable

In addition to this you can

In addition to this you can replace that line with:

DateTime dummydate = DateTime.MinValue;

Note that there was no camelcaps used with the dummyDate property afterwards so make sure that you change it to dummydate or dummDate accordingly.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Recent comments

User login

About our advertising.

Atom Feed

developer.* Blogs also has an Atom feed, located at this url.

Click here for more information about Atom.

A Jolt Award Finalist
Software Creativity 2.0
Foreword by Tom DeMarco

Recent Posters

Based on most recent 60 days, sorted by # of posts and name.

Google
Web developer.*

Who's online

There are currently 0 users and 14 guests online.

Syndicate

Syndicate content
All views expressed by authors, bloggers, and commentors are their own and do not necessarily reflect the views of developer.* or its proprietors.
Click to read the Copyright Notice.

All content copyright ©2000-2005 by the individual specified authors (and where not specified, copyright by Read Media, LLC). Reprint or redistribute only with written permission from the author and/or developer.*.

www.developerdotstar.com