Jan Schreuder on .Net

.Net code samples, experiences, observations

View my professional profile on LinkedIn

Recent Posts

Tags

News

  • Inappropriate comments will be deleted at my discretion.

    The information and code samples in this weblog is provided "AS IS" without warranty of any kind, either expressed or implied, including but not limited to the merchantability and/or fitness for a particular purpose.

Community

Email Notifications

Tool suppliers

Tools

General

Microsoft

Favorite blogs

Archives

Using VB.Net IsNumeric procedure in C#

In the C# project I'm doing now, I needed to know whether on string value contained a number or a date. I used a procedure such as this one, which works:

private bool IsNumeric(object value)
{
    bool result = false;
 
    try 
    {
        int i = Convert.ToInt32(value);
        result = true;
    }
    catch
    { 
        // Ignore errors 
    }
    return result;
}

And this works, obviously. But browsing the web, I found that some people (like Duncan MacKenzie) suggested using the Visual Basic Runtime in your C# project. Made curious by this I tried it myself and it works brilliantly. I can now use code that is shipped in the .Net Framework to do my testing. To do this yourself, simply follow these three steps:

  1. Add a reference to Microsoft Visual Basic .Net Runtime.
  2. Add Using Microsoft.VisualBasic; to the top of your code.
  3. Use Information.IsNumeric or Information.IsDate to check if your value contains a number or a date.

 

Comments

Jan Schreuder said:

I know even a GOOD C# has to tweak some vb in it's app to make it work
# August 27, 2004 7:11 PM

TrackBack said:

# August 27, 2004 7:20 PM

TrackBack said:

# August 27, 2004 7:23 PM

Jan Schreuder said:

Have not browsed that assembly yet with Reflector but if those methods use exceptin handling for testing if a specific object's string representation is representing a date, number, boolean and other datatypes then using it is really no option.

using exception handling for input checks == BAD :-)

Exception handling kill's performance especially when you are validating a textfile that contains a million lines containing 15 columns each to be validated this way.
# August 31, 2004 11:04 AM

Jan Schreuder said:

I agree that Exception handling slows things down. The point I'm trying to make however is to use the standard VB.Net checks in your C# project. The internals of the assembly are not relevant to me. I'm just happy I don't have to implement them myself.

I also agree with Duncan McKenzie (who pointed me in that direction) that it is likely that the checks in the VB.Net assembly are likely to be better than one you create yourself using exception handling. I also hope that MS will have made sure it's performs well enough.
# August 31, 2004 11:11 AM

TrackBack said:

# November 5, 2004 1:52 PM

TrackBack said:

# November 5, 2004 1:54 PM

Jan Schreuder said:

Using Try Catch for this kind of functionality is an absolute performance kill. if you really want to do it that way, use functions such as [bool Double.TryParse(String, out double)] (same for all other types), which return true if the conversion is successful. It's a much better approach.
# November 29, 2004 11:40 PM

Jan Schreuder said:

I know Antonio. The argument I was trying to make here is to use methods that are available to you. In this case, there is a Microsoft Visual Basic method which you can safely call from C#.
# November 30, 2004 7:40 AM

Jan Schreuder on .Net said:

Author : <a href="/blogs/jschreuder/">Jan Schreuder</a><br><br>
After years of experience as a developer in multiple languages, Jan shares his vision on software development with the world.<br>
# August 1, 2005 12:48 AM

Jan Schreuder on .Net said:

Author : Jan Schreuder After years of experience as a developer in multiple languages, Jan shares his vision on software development with the world.

# June 13, 2007 9:02 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 3 and 2 and type the answer here: