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:
- Add a reference to Microsoft Visual Basic .Net Runtime.
- Add Using Microsoft.VisualBasic; to the top of your code.
- Use Information.IsNumeric or Information.IsDate to check if your value contains a number or a date.