Problem using TryParse and NumbericUpDown

ADO DOT NET

Centurion
Joined
Dec 20, 2006
Messages
160
I always get invalid number error while using this:
Visual Basic:
        Dim MyRes As Boolean
        Dim MyInt As Integer = Integer.TryParse(NumericUpDown1.Value, MyRes)
        If MyInt < 1 Or MyInt > 65535 Then
            MsgBox("invalid number")
        End If
I am using VB.NET 2005, .NET Framework 2.0.
 

Attachments

I believe you've got your Boolean and Integer swapped.
Try this:
Visual Basic:
Dim MyInt As Integer
        Dim MyRes As Boolean = Integer.TryParse(NumericUpDown1.Value, MyInt)
        If MyInt < 1 Or MyInt > 65535 Then
            MsgBox("invalid number")
        End If

The second param to TryParse is the parsed integer value. The return of the function is true/false on whether the parsing worked.

Also, it looks like you want to parse a short integer. Maybe replace Integer with UShort above. Then you'd only have to check against 0 (if that's an invalid value for you). Just an observation, not critical to your question.

-ner
 
Just to add - putting
Visual Basic:
Option Strict On
at the top of your source files will catch a lot of these mistakes at compile time rather than run time.
 
Back
Top