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

Today, I ran into a strange little issue regarding the System.Uri class that took me too long to figure out. The Uri class has a property called Query that, according to the documentation, returns everything from the "?" to the end of the URI.

Consider this unescaped URI:

string url = "http://www.google.com/search?q=httpcompression c# asp.net";

string query = new Uri(url).Query;

The Uri.Query on that URI returns q=httpcompression c. As you can see, it breaks when it encounters a "#" character, so it doesn’t return everything from the "?" to the end. If the URI was escaped or UrlEncoded, there would have been no problem because the "#" character would be encoded into "%23". The problem is, that in this particular case I had to deal with unescaped URIs. 

So to make the above code work properly, the simplest way was to do exactly what the documentation for the URI.Query says: Return everything from the question mark to the end of the URI. That lead to the following helper method.

string url = "http://www.google.com/search?q=httpcompression c# asp.net";

string query = ExtractQuery(url);

 

public static string ExtractQuery(string url)

{

  if (string.IsNullOrEmpty(url))

    return string.Empty;

 

  int index = url.IndexOf("?") + 1;

  if (index == 0)

    return string.Empty;

 

  return url.Substring(index);

}

>

>

It’s not bad or ugly in any way, but it is annoying and also frustrating that the documentation isn’t correct either.