Simpler Coding?

Is there a simpler, more efficient way to code the toppings section, while still maintaining checkboxes?I am trying to get away from themultiple If/ElseIf/Then statements, if possible.

Thanks!

Private Sub btnOrderTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOrderTotal.Click

Dim pizzaCost As Single = 0

' Base cost of pizza, based on size

If radSmall.Checked = True Then

pizzaCost += 12.99

ElseIf radMedium.Checked = True Then

pizzaCost += 14.99

ElseIf radLarge.Checked = True Then

pizzaCost += 16.99

Else

MessageBox.Show("Please choose a pizza size.")

End If

' Add on additional costs for toppings

Const sngTOPPING_CHARGE As Single = 0.25

If chkPepperoni.Checked = True Then

pizzaCost += sngTOPPING_CHARGE

End If

If chkBlkOlives.Checked = True Then

pizzaCost += sngTOPPING_CHARGE

End If

If chkSausage.Checked = True Then

pizzaCost += sngTOPPING_CHARGE

End If

If chkOnion.Checked = True Then

pizzaCost += sngTOPPING_CHARGE

End If

If chkGrnOlive.Checked = True Then

pizzaCost += sngTOPPING_CHARGE

End If

If chkGrnPepper.Checked = True Then

pizzaCost += sngTOPPING_CHARGE

End If

If chkCanBacon.Checked = True Then

pizzaCost += sngTOPPING_CHARGE

End If

If chkPineapple.Checked = True Then

pizzaCost += sngTOPPING_CHARGE

End If

If chkMushroom.Checked = True Then

pizzaCost += sngTOPPING_CHARGE

End If

If chkHamburger.Checked = True Then

pizzaCost += sngTOPPING_CHARGE

End If

' Total cost of pizza

lblTotal.Text = "Cost: " & FormatCurrency(pizzaCost).ToString

End Sub

[6912 byte] By [SaberWeb] at [2007-12-28]
# 1

Homework time?

The way to simpify it is to use a class. You would have a Pizza class that would get modified by the appropriate events thus eliminating the need for the code you have here.

FrankCarr at 2007-9-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

you could also perhaps go through each checkbox and see if any item has been checked, if so then do your thing. So if we all had checkboxes in a group box called "toppings" we could potentially do this:

for each currentControl as CheckBox in Me.grpToppings.Controls

if currentControl.Checked = true then

pizzaCost += sngTOPPING_Charge

end if

next

alternatively on each checked/clicked event you could add/remove the charge on the pizza

ahmedilyas at 2007-9-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3

If you want to eliminate all but one of the IF statements. The following uses the tag property to store the cost of the individual items. So each item could be a different price.

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.CheckBox1.Tag = 10.1
Me.CheckBox2.Tag = 5.2
Me.CheckBox3.Tag = 6.3
End Sub

Private Sub CheckBox_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, CheckBox3.CheckedChanged
Dim total As Single = 0
For Each c As CheckBox In Me.GroupBox1.Controls
If c.Checked Then
total += CType(c.Tag, Single)
End If
Next
Label1.Text = total.ToString
End Sub
End Class

spotty at 2007-9-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

Thanks for your help! That looks much better than a bunch of if/elseif/then statements.

SaberWeb at 2007-9-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...