When I first switched from programming in Visual Basic to programming in C#, I felt a little neglected. There were a few basic functions that were not available to me in C# that were available in VB. One of these wonderful functions is IsNumeric. We all know that we can’t trust end users to enter the correct thing in our wonderful textboxes on our forms. Therefore, we have to check. In VB it was as simple as calling IsNumeric and popping in whatever I wanted to check. In C#, I had to write more than the 9 letters of IsNumeric to do the same thing. If you are a newbie to C# and you have experience in VB, I’m sure you have looked around for that function in vain.
There were some happy smiles around the Internet when C# programmers thought that it was coming in the next version of dot NET. Oh well. You can wipe those happy smiles off of your faces. As you may or may not know, it just ain’t happenin’ folks! (Please don’t email me to correct my English. I say what I want. I’m a woman) So since it’s not already in C#, and it’s not coming to C#, what are we going to do? There are so many solutions floating around the Internet that I’m sure you can find one quickly, however, I say if you can’t beat’em join’em.
Here is where References come in handy. It’s not really earth shattering, but I’m sure that someone somewhere may find it useful. So put away your RegEx stuff and stop writing your own class and let dot NET do all of the work for you. I’m so lazy right?
At the top of your code-behind in your using section, add a reference to Microsoft.VisualBasic. It should look like this:
using Microsoft.VisualBasic;
Now in your code, you can reference IsNumeric like this:
bool bYourVariable = true;
//cast the variable to string to avoid an error if it really is a string
//the error you get if you don’t cast it first is that the string
//isn’t formatted properly
string whateverYouAreCheckingGoesHere.ToString();
bYourVariable = Information.IsNumeric(whateverYouAreCheckingGoesHere);
That’s it! Now you can laugh in the faces of our VB programming brothers and sisters all around the world and tell them that you have found the IsNumeric in C#. You have put the eggs back in the cake and you can bake! Tell them that C# rocks. Then go back to doing whatever it was that you were doing!
Smooches,
Kila
Summary
Using the IsNumeric function in C#
Error/Problem:
C# does not have a built in IsNumeric function
Solution:
Create a reference to Microsoft.VisualBasic and then use Information.IsNumeric(whateverYouAreCheckingGoesHere)
This blog is for all Microsoft & .net lovers who may find themselves in need of help or information once in a while. We all have questions and we all need to know more than we know right now. When I come across things that I think may be useful, I post it. When I have something to say that I think is important, which is everything, I post it. All of my C#, ASP.NET, VB, AJAX & programming friends around the world are welcome to the info in my blog - Ramblings Of A Crazy DotNet Woman!
Tuesday, November 20, 2007
Where Is IsNumeric In C#? Right Where It Should Be!
Labels:
C# and IsNumeric,
C# equivalent of IsNumeric,
IsNumeric,
IsNumeric in C#,
using IsNumeric in C#
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment