Visual Basic Zip Solution Ch. 8 Lesson B Exercise 15

I am coding a Button that displays the shipping charge when you enter a zip code. To be valid, the ZIP code must contain exactly 5 digits, and the first three digits must be either "605" or "606". The shipping charge for "605" ZIP codes is $25. The shipping charge for "606" ZIP codes is $30. Display an appropriate message if the ZIP code is invalid. I am having trouble finding a way to have the right price show up for the two different zip codes. Can anyone help me out?


Private Sub btnDisplay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisplay.Click
        ' variable for shipping charge
        Dim strZip As String

        'remove any leading and trailing spaces
        strZip = txtZip.Text.Trim

        ' shipping price for zip code starting with 605
        If strZip Like "60[5-6]##" Then
            lblShipping.Text = "$25"



        Else
            MessageBox.Show("The Zip is invalid.", _
                            "Zip Code", MessageBoxButtons.OK, _
                            MessageBoxIcon.Information)

        End If

     

    End Sub
anault24Asked:
Who is Participating?
 
Éric MoreauSenior .Net ConsultantCommented:
You could try:

Dim strZip As String = txtZip.Text.Trim
if strzip.length = 5 then
   if strzip.startwith("605")
            lblShipping.Text = "$25" 
   elseif strzip.startwith("606")
            lblShipping.Text = "$30" 
    Else
            MessageBox.Show("The Zip is invalid.", _
                            "Zip Code", MessageBoxButtons.OK, _
                            MessageBoxIcon.Information)

        End If
    Else
            MessageBox.Show("The Zip is invalid.", _
                            "Zip Code", MessageBoxButtons.OK, _
                            MessageBoxIcon.Information)

        End If

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.