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??
Printable View
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??
VB Code:
Dim x As Double If IsNumeric(TextBox1.Text) Then x = TextBox1.Text MsgBox(x) End If
VB Code:
Imports Microsoft.VisualBasic.Information Dim num1 As Integer = 10 If IsNumeric(num1) Then 'It is numeric Else 'It is not numeric End If
Thanks!:D
This is the .NET WAY :
VB Code:
Try Dim int As Integer = Integer.Parse(Me.TextBox1.Text) Catch x As Exception MessageBox.Show("Please input numeric value") End Try
Have you ever tried using a NumericUpDown? They can't type letters into it, thus saving you the problem. ;)
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 ! ;)Quote:
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.
look at VIP's replies
This is a hangover from VB6. It will not work in .NET unless Option Strict is OFF. You would have to useQuote:
Originally posted by Negative0
VB Code:
Dim x As Double If IsNumeric(TextBox1.Text) Then x = TextBox1.Text MsgBox(x) End If
x=val(TextBox1.Text) or x=cdbl(TextBox1.Text)