Results 1 to 9 of 9

Thread: IsNumeric [RESOLVED]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131

    IsNumeric [RESOLVED]

    There is a way to verify that the contents of the text box are indeed numbers before putting them into variables. I know it's a function called IsNumeric. Can anyone post a code example on how to use it??
    Last edited by twisted; Jun 23rd, 2004 at 12:13 PM.
    Twisted

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    VB Code:
    1. Dim x As Double
    2.         If IsNumeric(TextBox1.Text) Then
    3.             x = TextBox1.Text
    4.             MsgBox(x)
    5.         End If

  3. #3
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    VB Code:
    1. Imports Microsoft.VisualBasic.Information
    2.  
    3. Dim num1 As Integer = 10
    4.  
    5. If IsNumeric(num1) Then
    6.    'It is numeric
    7. Else
    8.    'It is not numeric
    9. End If
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Thanks!
    Twisted

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    This is the .NET WAY :
    VB Code:
    1. Try
    2.             Dim int As Integer = Integer.Parse(Me.TextBox1.Text)
    3.         Catch x As Exception
    4.             MessageBox.Show("Please input numeric value")
    5.         End Try

  6. #6
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up

    Have you ever tried using a NumericUpDown? They can't type letters into it, thus saving you the problem.
    ~Peter


  7. #7
    Lively Member TLord's Avatar
    Join Date
    Jun 2004
    Posts
    95
    I don't recommend using Pirate's way.
    Try...Catch...End Try statement is used for watching a specific are of code for throwing an exception, in other words for identifying exceptions and responding to them, not for such small type situations. I recommend using the VB.Net's built is IsNumeric function, using such function saves time and lines.
    You may not find this a big problem, but what will you do if you have a program under development? where you type handreds of lines daily?
    I recommend also using Try...Catch...End Try statement for covering a whole subroutine and catching specific errors and exceptions.
    Last edited by TLord; Jun 28th, 2004 at 03:57 PM.
    Do you think my life is easy?
    Do you think it's good to win?
    do you think it's nice to kill?
    Do you think learning is a must?
    Do you think computers are nothing?
    Do you think this post is stupid?
    Do ypu think we're really humen?

    DO YOU THINK IT'S GOOD TO THINK AT ALL? ? ? ! ! !

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by TLord
    I don't recommend using Pirate's way.
    Try...Catch...End Try statement is used for watching a specific are of code for throwing an exception, in other words for identifying exceptions and responding to them, not for such small type situations. I recommend using the VB.Net's built is IsNumeric function, using such function saves time and lines.
    You may not find this a big problem, but what will you do if you have a program under development? where you type handreds of lines daily?
    I recommend also using Try...Catch...End Try statement for covering a whole subroutine and catching specific errors and exceptions.
    IsNumeric is not pure .NET function . It's only for compatibilitiy issues . Everybody expect that MS will stop supporting this class (Microsoft.VisualBasic) in the future releases . As for the the Parse method it's the only pure code that does the job ! There's no problem with Try...Catch block as long as the conversion is applicable . I always use this way and it's been working great . Never fail me !

    look at VIP's replies

  9. #9
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by Negative0
    VB Code:
    1. Dim x As Double
    2.         If IsNumeric(TextBox1.Text) Then
    3.             x = TextBox1.Text
    4.             MsgBox(x)
    5.         End If
    This is a hangover from VB6. It will not work in .NET unless Option Strict is OFF. You would have to use

    x=val(TextBox1.Text) or x=cdbl(TextBox1.Text)
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

Posting Permissions

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



Click Here to Expand Forum to Full Width