0

Firstly I would like to say this was a homework assignment I was working on, but couldn't get it to work this way. So I had to complete the assignment using If statements. The objective was to build essentially a form that a user could fill out to submit an order. There are three check boxes (one for burgers, one for fries, and another for drinks). If you check one of the boxes a group box will then become visible containing radio buttons to make the selection you want.

I wanted to try to use a select statement based on if the check box had been checked. Then use different cases for each of the radio buttons. However if I run the program and click the one button I supplied it will populate my dblCost variable with the first Case available. Below is my code. I just really want to understand what I was doing wrong or if this isn't a feasible way of approaching this problem. Below is the code I wanted to use:

Public Class frmRestaurantOrder

Private Sub CheckedChanged(sender As Object, e As EventArgs) Handles cbxBurgers.CheckedChanged, cbxFries.CheckedChanged, cbxDrinks.CheckedChanged

    If (cbxBurgers.Checked) Then
        gbxBurgers.Visible = True
    Else
        gbxBurgers.Visible = False
    End If

    If (cbxFries.Checked) Then
        gbxFries.Visible = True
    Else
        gbxFries.Visible = False
    End If

    If (cbxDrinks.Checked) Then
        gbxDrinks.Visible = True
    Else
        gbxDrinks.Visible = False
    End If

End Sub

Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click

    Dim dblCost As Double = 0

    Select Case gbxBurgers.Visible = True
        Case rbtRegularBurger.Checked
            dblCost += 4.19
        Case rbtCheeseBurger.Checked
            dblCost += 4.79
        Case rbtBaconBurger.Checked
            dblCost += 4.79
        Case rbtBaconCheeseBurger.Checked
            dblCost += 5.39
        Case Else
            dblCost += 0
    End Select

    Select Case cbxFries.Checked = True
        Case rbtSmallFries.Checked
            dblCost += 1.29
        Case rbtLargeFries.Checked
            dblCost += 1.59
        Case Else
            dblCost += 0
    End Select

    Select Case cbxDrinks.Checked = True
        Case rbtSoda.Checked
            dblCost += 1.69
        Case rbtWater.Checked
            dblCost += 1.49
        Case Else
            dblCost += 0
    End Select

    txtCost.Text = FormatCurrency(dblCost)

End Sub

End Class

Dion Pezzimenti
  • 243
  • 2
  • 11
  • I am not sure I understand why you are testing the first case for visibility and the rest for checked. Best way to learn what is going on is to debug. Do you know how to do that? – ron tornambe Mar 27 '13 at 00:20

2 Answers2

0

I think your understanding of select case is a little off. Take a look at this and see if it helps.

ToddB
  • 1,464
  • 1
  • 12
  • 27
0

While it doesn't specifically address the question, you may also want to consider a shorter way to write your code:

Rather than:

If (cbxBurgers.Checked) Then
    gbxBurgers.Visible = True
Else
    gbxBurgers.Visible = False
End If

You can write:

gbxBurgers.Visible = cbxBurgers.Checked

It's a small thing, but it makes the code a lot shorter.

Derek Tomes
  • 3,989
  • 3
  • 27
  • 41