IsNumeric in C#-VBForums
Results 1 to 13 of 13

Thread: IsNumeric in C#

  1. #1

    Thread Starter
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,140

    Post IsNumeric in C#

    a simple example of implementing the IsNumeric function in to C# i knocked up ...
    VB Code:
    1. [color=blue]private bool[/color] [color=black]IsNumeric[/color]([color=blue]object[/color] ValueToCheck)
    2. {
    3.     [color=blue]double[/color] Dummy = [color=blue]new double[/color]();
    4.     [color=blue]string[/color] InputValue = Convert.ToString(ValueToCheck);
    5.  
    6.     [color=blue]bool[/color] Numeric = [color=blue]double[/color].TryParse( InputValue , System.Globalization.NumberStyles.Any , [color=blue]null[/color] , [color=blue]out[/color] Dummy);
    7.        
    8.     [color=blue]return[/color] Numeric;
    9. }
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  2. #2
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    I think there is a built in Function in .net, you should be able to achieve the same with

    Char.IsNumber() method.
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  3. #3
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,092
    Originally posted by Danial
    I think there is a built in Function in .net, you should be able to achieve the same with

    Char.IsNumber() method.
    That would only work for single charactersm which means you would have to do a loop for the whole string. Not to mention checking for decimals and such.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    Originally posted by crptcblade
    That would only work for single charactersm which means you would have to do a loop for the whole string. Not to mention checking for decimals and such.
    Yes you are right. I was thinking it cheks the whole string instead of Char.

    My apology
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I hate to be the person that recommends this, but if you need the method, it already exists, so why re-create it. You can find it here:

    MessageBox.Show(Microsoft.VisualBasic.Information.IsNumeric(textBox1.Text));

    You just have to reference the Microsoft Visual Basic Runtime.

  6. #6
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252

    Re: IsNumeric in C#

    Is it okay to import VB to C#?

    C# executes faster than VB.NET according to http://www.vbconversions.com/topten.aspx.

    If that's true, I would not prefer using IsNumeric from Visual Basic. I would rather use a natice C# method.

  7. #7
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,705

    Re: IsNumeric in C#

    The link you provided that supposedly says C# is faster than vb... lol... take a look at the VB source code they used (see below). They do not explicity declare the variables as ints or doubles, they just leave them as generic objects. Well hell, in code like this that does a loop accessing those properties of the objects, of course it will run slower.

    VB Code:
    1. If IsNumeric(Me.txtMaxNbr.Text) = False Then
    2.             MsgBox("Max Nbr must be numeric!", MsgBoxStyle.Exclamation, "Max Nbr Not Numeric")
    3.             Return
    4.         End If
    5.  
    6.         [b]Dim N, i, j[/b] '<------ WHAT THE HELL
    7.         N = CInt(Me.txtMaxNbr.Text)
    8.         Dim z(N)
    9.         Dim startTime As DateTime = Now
    10.  
    11.         'initialize array
    12.         For i = 0 To N
    13.             z(i) = 0
    14.         Next
    15.  
    16.         'mark multiples of i
    17.  
    18.         For i = 2 To N / 2
    19.             For j = 2 * i To N Step i
    20.                 z(j) = 1
    21.             Next
    22.         Next
    23.  
    24.         'count unmarked numbers, which are primes
    25.         Dim nbr = 0
    26.  
    27.         For i = 2 To N
    28.             If z(i) = 0 Then
    29.                 nbr += 1
    30.             End If
    31.         Next
    32.  
    33.         Dim ts As New TimeSpan(Now.Ticks - startTime.Ticks)
    34.  
    35.         Me.lblNbrPrimes.Text = nbr
    36.         Me.lblTime.Text = ts.ToString

  8. #8
    Hyperactive Member
    Join Date
    Dec 2002
    Location
    The Big D
    Posts
    310

    Re: IsNumeric in C#

    Quote Originally Posted by nemaroller
    The link you provided that supposedly says C# is faster than vb... lol... take a look at the VB source code they used (see below). They do not explicity declare the variables as ints or doubles, they just leave them as generic objects. Well hell, in code like this that does a loop accessing those properties of the objects, of course it will run slower.

    Also, if you compile the VB code in release mode, I got 1.68 secs for the VB version (even with the lousy variable decs). Compare this to 1.67 for C# and
    Last edited by VBGuy; Feb 28th, 2005 at 08:30 PM.

  9. #9
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,983

    Re: IsNumeric in C#

    To make it cleaner, why not this:

    VB Code:
    1. private bool IsNumeric(object ValueToCheck)
    2.         {
    3.             double Dummy = 0;
    4.             return double.TryParse(ValueToCheck.ToString() , System.Globalization.NumberStyles.Any , null , out Dummy);
    5.         }

  10. #10
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,032

    Re: IsNumeric in C#

    Quote Originally Posted by hellswraith
    I hate to be the person that recommends this, but if you need the method, it already exists, so why re-create it. You can find it here:

    MessageBox.Show(Microsoft.VisualBasic.Information.IsNumeric(textBox1.Text));

    You just have to reference the Microsoft Visual Basic Runtime.
    I went looking for another IsNumeric function when I discovered that

    Code:
     
    Microsoft.VisualBasic.Information.IsNumeric("010164+")
    returns True.

    The following function gives the desired result:

    Code:
    Public Function IsNumeric(ByVal inputString As String) As Boolean
            Dim _isNumber As System.Text.RegularExpressions.Regex = New _
                    System.Text.RegularExpressions.Regex("(^[-+]?\d+(,?\d*)*\.?\d*([Ee][-+]\d*)?$)|(^[-+]?\d?(,?\d*)*\.\d+([Ee][-+]\d*)?$)")
    
            Return _isNumber.Match(inputString).Success
    End Function
    which I found in this thread:

    http://www.vbforums.com/showthread.php?t=394507

  11. #11
    Frenzied Member
    Join Date
    May 2006
    Location
    Toronto, ON
    Posts
    1,093

    Re: IsNumeric in C#

    The simplest way would be to do something like this:

    Code:
    string s = "<whatever>";
    
    try
    {
        int i = int.Parse(s)
        ... <Is numeric stuff here>
    }
    catch
    {
        ... <Is not numeric stuff here>
    }
    I'm pretty sure that's how the IsNumeric function is coded in VB as well. I know that if you have Visual Studio break on all exceptions, the IsDate function throws an error when it finds a non-date, so I assume it's the same for IsNumeric.

  12. #12
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,705

    Re: IsNumeric in C#

    Better way would be attempting to match a compiled Regex ^\d$+$ against the string.

    This way you don't get a performance hit by catching an exception.

    Or, tryParse in C# 2.0, which avoids throwing exceptions.

  13. #13
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,032

    Re: IsNumeric in C#

    Quote Originally Posted by nemaroller
    Better way would be attempting to match a compiled Regex ^\d$+$ against the string.

    This way you don't get a performance hit by catching an exception.

    Or, tryParse in C# 2.0, which avoids throwing exceptions.
    That's right. It is considered poor programming practice to capture exceptions as part of the logic when there is some other way of testing first. For example, when dividing two numbers, it is better practice to test the denominator for a zero value than to catch a divide by zero error.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.