Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IsNumeric C# Equivalent

Status
Not open for further replies.

junwebhead

Programmer
Jun 13, 2003
41
QA
Hi everyone!
I hope someone can help me. I just want to know if there is a C# equivalent method for VB's IsNumeric function? I've tried searching for it but I can't find anything other than try-catch - Int32.Parse() combination. I'm thinking of doing my own function with it. But I'm just wondering that there maybe a built-in method for that.
Also, is there a site in which tells how to do some code snippet in C# and VB6 side by side? For example,

this is how you do it in VB6....
' some VB6 code about something...

then this is how you do it in C#...
// some C# equivalent code...

I think someone could have thinking of doing it because of the huge number of VB'ers that migrated to C#.

Thanks!!!

Jun
 
I havn't found an answer to IsNumeric() alt yet. But you can consider this question while we're both waiting for a better alternative to try/catch blocks: How often do you need to parse a non-numeric value? Normally, we display numeric values from a datasource (database or memory) with explicit data type. So, no need to parse when displaying that value. And, we often receive inputs from our user interfaces (eg. textboxes). Rather than code your own function to parse, maybe you need to design your own textbox to accept valid chars only. Well, of course, that depends on the project your working. Just sharing my thoughts! [peace]
is there a site in which tells how to do some code snippet in C# and VB6 side by side?
YES! Check-out the banner on your uppermost left corner! [wink]
(IME, I used to buy compilations of sourcecodes in CDs at APO, but I realized it's such a waste.)
 
junwebhead and alphanytz

I don't know if this would help you at all - but copy the following link into your MSDN Library (if you have it):

ms-help://MS.MSDNQTR.2003OCT.1033/vsintro7/html/vxoriLanguageEquivalentsKeywords.htm

It's a comparison of different keywords in the different MS languages. The additional links at the bottom of the page might also prove useful

Hope this helps

Craftor

:cool:
 
Thanks!
Actually, I need the IsNumeric function to validate a textbox which contains a person's age.

I found this on
double resNum;
bool isNum = Double.TryParse( "1234", NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out resNum );

But I think I will just create a function IsNumeric using try catch.

Thanks for the thoughts.
I know this is stupid but I can't find the banner you were talking about. Did you mean Tek-Tips?
[glasses]

Jun
 
Thanks Craftor.

That was great! but I haven't found it using exactly the url you've given. Mine is on
ms-help://MS.MSDNQTR.2003FEB.1033/vsintro7/html/vxorilanguageequivalentskeywords.htm

I ran the MSDN update a couple of weeks now. But I think the dreaded "Help is updating to reflect your latest changes " that appear everytime I run MSDN wiped out the updates. Thanks anyways.

Jun
 
How about only allowing numeric values in the text box ?

Edit the key press event for the text box and add:

private void textBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
e.Handled = !Char.IsDigit(e.KeyChar); // only accept numbers
if (e.KeyChar == 127) e.Handled = false; // backspace
if (e.KeyChar == 8) e.Handled = false; // backspace
if (e.KeyChar == 13)
{
// some event fired by return
}
}

works a bit better than catching an exception ...

Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top