0

Am using program which must treat user input differently, depends whether on number or string. Select Case and IsNumeric are not working as expected.

I get this code when animal=a char or string.

Error:

An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

Additional information: Conversion from string "D" to type 'Long' is not valid.

Code which troubles:

Case "D" Or "d"

All of code:

Option Explicit Off
Option Strict Off

Public Class MainForm

Public Sub ifButton_Click(sender As Object, e As EventArgs) Handles ifButton.Click

    animal = codeTextBox.Text
    Select Case IsNumeric(codeTextBox.Text)
        Case True
            Dim decanimal As Decimal
            decanimal = CDec(animal)
            Select Case decanimal
                Case "1"
                    msgLabel.Text = "Dog"
                Case "2"
                    msgLabel.Text = "Cat"
                Case Else
                    msgLabel.Text = "Bird"
            End Select
        Case False
            Dim stranimal As String
            stranimal = CStr(animal)
            Select Case stranimal
                Case "D" Or "d"
                    msgLabel.Text = "Dog"
                Case "C" Or "c"
                    msgLabel.Text = "Cat"
                Case Else
            End Select
    End Select

End Sub
End Class
Community
  • 1
  • 1

3 Answers3

3

You should look at the documentation for Select Case you don't put Or, you put a comma.

Case "D", "d"

Or put the compared string in lower case.

Select Case stranimal.ToLower()
    Case "d"
        msgLabel.Text = "Dog"
    Case "c"
        msgLabel.Text = "Cat"
    Case Else
End Select

decanimal is a decimal, don't use string in your case statement. Also, turn that option strict on ;)

the_lotus
  • 12,668
  • 3
  • 36
  • 53
1

You could use the return value of Double.TryParse() to see if the data is numeric. Here's the corrected code:-

Public Sub ifButton_Click(sender As Object, e As EventArgs) Handles ifButton.Click
    Dim animal As String = codeTextBox.Text
    Select Case Double.TryParse(animal, Nothing) 'See if it is convertible to Double (numeric) or not.
        Case True
            Select Case Double.Parse(animal)

                Case 1
                    msgLabel.Text = "Dog"
                Case 2
                    msgLabel.Text = "Cat"
                Case Else
                    msgLabel.Text = "Bird"
            End Select
        Case False
            Select Case animal.ToLower() 'To compare the strings without case-sensitivity
                Case "d"
                    msgLabel.Text = "Dog"
                Case "c"
                    msgLabel.Text = "Cat"
                Case Else
                    'You didn't mention anything but I guess it should be msgLabel.Text = "Bird"
            End Select
    End Select
End Sub
Fᴀʀʜᴀɴ Aɴᴀᴍ
  • 6,131
  • 5
  • 31
  • 52
0

An alternative coding approach...

    Dim cAnimals As New Collection
    cAnimals.Add("Dog", "d")
    cAnimals.Add("Dog", "1")
    cAnimals.Add("Cat", "c")
    cAnimals.Add("Cat", "2")

    Dim s As String = ""
    Do While True
        s = InputBox("code:").ToLower
        If s = "" Then Exit Do
        If cAnimals.Contains(s) Then
            MsgBox(cAnimals(s))
        Else
            MsgBox("Invalid code")
        End If
    Loop

Use some data structure to store codes for conversion, here a VB Collection, then check to see if the code is in the data or not.

VB doesn't really care if the data is numeric or not. This can bite at times but is useful at other times. Option Strict will defeat, not really needed IMO for most cases. Depends on the importance of the app and locally policy.

rheitzman
  • 2,247
  • 3
  • 20
  • 36