Dim str As String = "100"
Dim int As Integer = CInt(str)
Dim obj As Object = 100
Dim int As Integer = CInt(obj)
While it is valid, I never use CInt to convert a value that is not an Integer into an Integer. I use it strictly for casting. For instance, I would never do this:because that's converting a value that is a String into and Integer. I would do this though:VB.NET:Dim str As String = "100" Dim int As Integer = CInt(str)
because that's casting a value that is already an integer as type Integer. If I want to perform a genuine conversion then I'll use the Convert class or the Parse or TryParse method.VB.NET:Dim obj As Object = 100 Dim int As Integer = CInt(obj)
Use Integer.TryParse, as in:But what if the integer that needs to be used comes from user input from a textbox? Then you sort of have to do something like this:
Dim inputFromUser as string = textbox1.text
if isnumeric(inputFromUser) = true
dim integerInput as integer = cint(inputFromUser)
else
msgbox ("Input is not a number")
end if
Dim myInt As Integer
If Integer.TryParse(Textbox1.Text.Trim, myInt) = True Then
'Use your integer variable because it's assigned already
Else
'Couldn't convert to an integer, alert the user
End If
Use Integer.TryParse, as in:VB.NET:Dim myInt As Integer If Integer.TryParse(Textbox1.Text.Trim, myInt) = True Then 'Use your integer variable because it's assigned already Else 'Couldn't convert to an integer, alert the user End If
Another thought, use a NumericUpDown control instead of a TextBox
For one IsNumeric is a legacy function. Also I'm not dimensioning the variable inside the If block (which means the variable isn't available outside of it). The TryParse function is part of the Integer class, which means it's directly tied to the data type that it needs to handle. Also it's doing the numeric checking and converting in one line, instead of two. Plus Integer.TyParse is easier to change code datatypes, because there's a Double.TryParse, Decimal.TryParse, so a simple Find&Replace "Integer" to "Decimal" is a snap.Any specific reason why your mehtod is better than mine? Seems like it does the same thing (both your code and my code tests if the input value is correct, so no exceptions will happen). Any performance increase?
I have been in situations where a numericupdown is not useful. But you're right, in most situations that control would probably be the best choice
IsNumeric cannot distinguish between different types of numbers for a start. Secondly, IsNumeric itself calls Double.TryParse. Thirdly, to determine whether a value is numeric, IsNumeric must parse it, so your code actually parses the same value twice to get a result.Any specific reason why your mehtod is better than mine? Seems like it does the same thing (both your code and my code tests if the input value is correct, so no exceptions will happen). Any performance increase?
I have been in situations where a numericupdown is not useful. But you're right, in most situations that control would probably be the best choice
Plus Integer.TyParse is easier to change code datatypes, because there's a Double.TryParse, Decimal.TryParse, so a simple Find&Replace "Integer" to "Decimal" is a snap.
I've never ran into a situation where a NumericUpDown for numeric input doesn't work, from the end user point of view a NUD is a textbox with convenient up & down buttons (spinners), how can a NUD not ever work?
You don't have to, but here's a little more about what I was getting at:Sorry, I don't buy that one, as it's the same with cint. There's a cint (for integer), cdbl (for double), cdec (for decimal), etc. It's just as easy to do a find&replace with cint than it is with integer.tryparse![]()
Private Sub DoSomething
Dim myNumber1, myNumber2, myNumber3 As [B]Integer[/B]
If [B]Integer[/B].TryParse(Number1TextBox.Text.Trim, myNumber1) = True Then
'Some code
End If
If [B]Integer[/B].TryParse(Number2TextBox.Text.Trim, myNumber2) = True Then
'Some code
End If
If [B]Integer[/B].TryParse(Number3TextBox.Text.Trim, myNumber3) = True Then
'Some code
End If
End Sub
Ah, but that means it's not numeric only input anymore. The scenario is numeric only which a NUD handles, if you need characters other than the numbers 0 to 9 and possibly a decimal point then yes you need a textbox because it's not numeric only input.Well, in situations where the user also could input text into the same box and NUD wouldn't work. I had a situation where they needed a box where if the value in the box was a number it should call methodA, but if it was text it should call methodB. In that specific situations having one textbox and one NUD at the same time wasn't an option
When you code .NET you should avoid VB-specific terms intended to make old programmers and legacy code work without too much manipulation. While it would be acceptable to start out with old VB6 code and upgrade it, relying on CInt etc, if you use only methods in common with other .NET syntaxes, it will make it easier for you to:Sorry, I don't buy that one, as it's the same with cint. There's a cint (for integer), cdbl (for double), cdec (for decimal), etc. It's just as easy to do a find&replace with cint than it is with integer.tryparse![]()
And the TryParse method would solve it perfectly: if it parsed to a number, do number, if it didnt, do text.In that specific situations having one textbox and one NUD at the same time wasn't an option