In many different scenarios we need to check if a string can be converted into an integer e.g. This could be when we work with query strings and need to check if they match a certain data type. In VB.NET you can use the IsNumeric and IsDate functions, but that's about it. You are left to your own data type checking logic the rest of the time. It would be cool if we could have a method that could check all data types that is represented by strings such as integers, guids, booleans etc.

Here is a method that does just that. It can check all the string based types and also enums.

   
/// <summary> /// Checks the specified value to see if it can be /// converted into the specified type. /// <remarks> /// The method supports all the primitive types of the CLR /// such as int, boolean, double, guid etc. as well as other /// simple types like Color and Unit and custom enum types. /// </remarks> /// </summary> /// <param name="value">The value to check.</param> /// <param name="type">The type that the value will be checked against.</param> /// <returns>True if the value can convert to the given type, otherwise false.</returns> public static bool CanConvert(string value, Type type) { if (string.IsNullOrEmpty(value) || type == null) return false; System.ComponentModel.TypeConverter conv = System.ComponentModel.TypeDescriptor.GetConverter(type); if (conv.CanConvertFrom(typeof(string))) { try { conv.ConvertFrom(value); return true; } catch { } } return false; }

Example of use

Let's try some different examples

   
CanConvert("12", typeof(int)); // returns true CanConvert("f637a876-9d58-4229-9559-a5e42a95fdac ", typeof(Guid)); // returns true CanConvert("Backspace", typeof(System.ConsoleKey)); // returns true CanConvert("10px", typeof(System.Web.UI.WebControls.Unit)); // returns true CanConvert("red", typeof(System.Drawing.Color)); // returns true

In many cases we write our ASP.NET logic around query strings in order to show the right product page or what not. The first thing we do is to check if the query string exists in the first place before we start using it. It could look like this:

if (Request.QueryString["id"] != null)

{

  // Do something with the querystring

}

The only problem with the above check to see if the query string is null, is that we don’t take into consideration if the query string is filled or not. That could lead to unhandled exceptions in the code. Instead we should check for query strings like this:

if (!String.IsNullOrEmpty(Request.QueryString["id"]))

{

  // Do something with the querystring

}

Then there is the data type of the query string. Our code might need an integer of 5 digits to get the right information from the database, so if we pass it a string we could end up with a data type mismatch exception. So we do the check again more thoroughly:

if (!String.IsNullOrEmpty(Request.QueryString["id"]) && Request.QueryString["id"].Length == 5)

{

  // Do something with the querystring

}

Now we know that we get a query string suitable for further processing. You can then do more precise data type checks using the TryParse method of most value types or by some other logic.