Search code examples
vb.netselect-case

Age based "Select Case"


So I am trying to code a button that reads the age entered by the user and determines what price they receive. Below is how I have tried doing this with no luck.

 Private Sub btnSelect_Click(sender As Object, e As EventArgs) Handles btnSelect.Click
    Dim txtage As String
    txtage = lblPrice.Text.ToUpper
    Select Case txtage
        Case CStr(0) To CStr(3)
            lblPrice.Text = "0"
        Case CStr(4) To CStr(63)
            lblPrice.Text = "9"

    End Select

End Sub

I have Option Explicit On, Option Strict On, Option Infer Off,

What am I missing or doing wrong here? Please and thank you.


Solution

  • Then get the value from the right place, and use some code protection with Integer.TryParse since your reading from a string.

    Private Sub btnSelect_Click(sender As Object, e As EventArgs) Handles btnSelect.Click
      Dim age As Integer
      If Integer.TryParse(txtage.Text, age) Then
        Select Case age
          Case 0 To 3
            lblPrice.Text = "0"
          Case 4 To 63
            lblPrice.Text = "9"
        End Select
      End If
    End Sub