views:

215

answers:

4

I have created a simply Pizza program (for a summer project not education related) that uses check boxes to determine what the customer wants. Basically a simple pizza ordering program. I was wondering as I have used the If statement over and other again for different parameters and variables, is it possible to use multiply arrays to simply the VB Code...

I was advised to use Constructors in VB.Net, but have no current experience, could you help? Or is there a more simpler way to go about creating this program...or a better way of programing? What about decreasing the repetition in the code?

Option Strict On
Public Class Form1

Dim CurrentBalance As String
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

    Dim SmallPizza As String
    SmallPizza = "5"
    CurrentBalance = Label4.Text
    If CheckBox1.Checked Then
        ListBox1.Items.Add("Small Pizza")
        Label4.Text = CStr(Val(CurrentBalance) + Val(SmallPizza))
    Else
        ListBox1.Items.Remove("Small Pizza")
        Label4.Text = CStr(Val(CurrentBalance) - Val(SmallPizza))
    End If
End Sub

Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
    Dim MediumPizza As String
    MediumPizza = "7"
    CurrentBalance = Label4.Text
    If CheckBox2.Checked Then
        ListBox1.Items.Add("Medium Pizza")
        Label4.Text = CStr(Val(CurrentBalance) + Val(MediumPizza))
    Else
        ListBox1.Items.Remove("Medium Pizza")
        Label4.Text = CStr(Val(CurrentBalance) - Val(MediumPizza))
    End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    CurrentBalance = Label4.Text
    MessageBox("Total cost: £" & CurrentBalance)
End Sub

Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
    Dim LargePizza As String
    LargePizza = "9"
    CurrentBalance = Label4.Text
    If CheckBox3.Checked Then
        ListBox1.Items.Add("Large Pizza")
        Label4.Text = CStr(Val(CurrentBalance) + Val(LargePizza))
    Else
        ListBox1.Items.Remove("Large Pizza")
        Label4.Text = CStr(Val(CurrentBalance) - Val(LargePizza))
    End If
End Sub

Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox4.CheckedChanged
    Dim ExtraLargePizza As String
    ExtraLargePizza = "7"
    CurrentBalance = Label4.Text
    If CheckBox4.Checked Then
        ListBox1.Items.Add("Extra Large Pizza")
        Label4.Text = CStr(Val(CurrentBalance) + Val(ExtraLargePizza))
    Else
        ListBox1.Items.Remove("Extra Large Pizza")
        Label4.Text = CStr(Val(CurrentBalance) - Val(ExtraLargePizza))
    End If
End Sub

Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox5.CheckedChanged
    Dim Ham As String
    Ham = "1"
    CurrentBalance = Label4.Text
    If CheckBox5.Checked Then
        ListBox1.Items.Add("Ham")
        Label4.Text = CStr(Val(CurrentBalance) + Val(Ham))
    Else
        ListBox1.Items.Remove("Ham")
        Label4.Text = CStr(Val(CurrentBalance) - Val(Ham))
    End If
End Sub

Private Sub CheckBox6_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox6.CheckedChanged
    Dim Pineapple As String
    Pineapple = "1"
    CurrentBalance = Label4.Text
    If CheckBox6.Checked Then
        ListBox1.Items.Add("Pineapple")
        Label4.Text = CStr(Val(CurrentBalance) + Val(Pineapple))
    Else
        ListBox1.Items.Remove("Pineapple")
        Label4.Text = CStr(Val(CurrentBalance) - Val(Pineapple))
    End If
End Sub

Private Sub CheckBox7_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox7.CheckedChanged
    Dim Bananna As String
    Bananna = "1"
    CurrentBalance = Label4.Text
    If CheckBox7.Checked Then
        ListBox1.Items.Add("Bananna")
        Label4.Text = CStr(Val(CurrentBalance) + Val(Bananna))
    Else
        ListBox1.Items.Remove("Bananna")
        Label4.Text = CStr(Val(CurrentBalance) - Val(Bananna))
    End If
End Sub

Private Sub CheckBox8_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox8.CheckedChanged
    Dim Meat As String
    Meat = "1"
    CurrentBalance = Label4.Text
    If CheckBox8.Checked Then
        ListBox1.Items.Add("Meat")
        Label4.Text = CStr(Val(CurrentBalance) + Val(Meat))
    Else
        ListBox1.Items.Remove("Meat")
        Label4.Text = CStr(Val(CurrentBalance) - Val(Meat))
    End If
End Sub

Private Sub CheckBox9_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox9.CheckedChanged
    Dim ExtraCheese As String
    ExtraCheese = "1"
    CurrentBalance = Label4.Text
    If CheckBox9.Checked Then
        ListBox1.Items.Add("Extra Cheese")
        Label4.Text = CStr(Val(CurrentBalance) + Val(ExtraCheese))
    Else
        ListBox1.Items.Remove("Extra Cheese")
        Label4.Text = CStr(Val(CurrentBalance) - Val(ExtraCheese))
    End If
End Sub

Private Sub CheckBox10_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox10.CheckedChanged
    Dim Pepperoni As String
    Pepperoni = "1"
    CurrentBalance = Label4.Text
    If CheckBox10.Checked Then
        ListBox1.Items.Add("Pepperoni")
        Label4.Text = CStr(Val(CurrentBalance) + Val(Pepperoni))
    Else
        ListBox1.Items.Remove("Pepperoni")
        Label4.Text = CStr(Val(CurrentBalance) - Val(Pepperoni))
    End If
End Sub

Private Sub CheckBox11_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox11.CheckedChanged
    Dim Special As String
    Special = "1"
    CurrentBalance = Label4.Text
    If CheckBox11.Checked Then
        ListBox1.Items.Add("Special")
        Label4.Text = CStr(Val(CurrentBalance) + Val(Special))
    Else
        ListBox1.Items.Remove("Special")
        Label4.Text = CStr(Val(CurrentBalance) - Val(Special))
    End If
End Sub

Private Sub CheckBox12_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox12.CheckedChanged
    Dim Pickup As String
    Pickup = "1"
    CurrentBalance = Label4.Text
    If CheckBox12.Checked Then
        ListBox1.Items.Add("Pickup")
        Label4.Text = CStr(Val(CurrentBalance) - Val(Pickup))
    Else
        Label4.Text = CStr(Val(CurrentBalance) + Val(Pickup))
        ListBox1.Items.Remove("Pickup")
    End If
End Sub

Private Sub CheckBox13_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox13.CheckedChanged
    Dim Deliver As String
    Deliver = "5"
    CurrentBalance = Label4.Text
    If CheckBox13.Checked Then
        ListBox1.Items.Add("Deliver")
        Label4.Text = CStr(Val(CurrentBalance) + Val(Deliver))
    Else
        Label4.Text = CStr(Val(CurrentBalance) - Val(Deliver))
        ListBox1.Items.Remove("Deliver")
    End If
End Sub

Private Sub ChesseToastToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChesseToastToolStripMenuItem.Click
    Dim CheeseToast As String
    CheeseToast = "8"
    CurrentBalance = Label4.Text
    ListBox1.Items.Add("Cheese Toast")
    Label4.Text = CStr(Val(CurrentBalance) + Val(CheeseToast))
End Sub

Private Sub GarlicToastToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GarlicToastToolStripMenuItem.Click
    Dim GarlicToast As String
    GarlicToast = "11"
    CurrentBalance = Label4.Text
    ListBox1.Items.Add("Garlic Toast")
    Label4.Text = CStr(Val(CurrentBalance) + Val(GarlicToast))
End Sub

Private Sub BreadSticksToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BreadSticksToolStripMenuItem.Click
    Dim Breadsticks As String
    Breadsticks = "14"
    CurrentBalance = Label4.Text
    ListBox1.Items.Add("Bread Sticks")
    Label4.Text = CStr(Val(CurrentBalance) + Val(Breadsticks))
End Sub

Private Sub CashToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CashToolStripMenuItem.Click
    ListBox1.Items.Add("Paying by Cash")
End Sub

Private Sub ChequeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChequeToolStripMenuItem.Click
    ListBox1.Items.Add("Paying by Cheque")
End Sub

Private Sub DebitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DebitToolStripMenuItem.Click
    ListBox1.Items.Add("Paying by Debit Card")
End Sub

Private Sub CreditCardToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreditCardToolStripMenuItem.Click
    ListBox1.Items.Add("Paying by Crebit Card")
End Sub

Private Sub VoucherToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VoucherToolStripMenuItem.Click
    Dim Voucher As String
    Voucher = "5"
    CurrentBalance = Label4.Text
    ListBox1.Items.Add("Voucher")
    Label4.Text = CStr(Val(CurrentBalance) - Val(Voucher))
End Sub

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
    End
End Sub

Private Sub MessageBox(ByVal p1 As String)
    Throw New NotImplementedException
End Sub

Help would be much appreciated.

+3  A: 

You can make a function for adding/removing items with parameters: name, price, checkboxInstance

  • name would contain name of an item (Voucher, Pickup, ...)

  • price would be used in to change value in Label4.Text

  • checkboxInstance would help you to decide what operation to do:

So the function's body would look like this:

  If checkboxInstance.Checked Then
       ListBox1.Items.Add(name)
       Label4.Text = Val(CurrentBalance) - Val(price)
  Else
      Label4.Text = Val(CurrentBalance) + Val(price)
      ListBox1.Items.Remove(name)
  End If

I don't really remember syntax of VB.NET. But as it is your homework I guess it's better to provide a hint than a code.

EDIT: There's one more thing how to radically simplify the code. You can use "tag" property of checkbox where you would save the name of goods and instead of having checkbox1, checkbox2,.. you can in designer of Visual Studio make an array of checkboxes so that you can index them (myCheckboxes[0], myCheckboxes[1], ...). http://www.thevbprogrammer.com/VBNET_09/09-05-ControlArrays.htm - check this website.

MartyIX
Thanks, I'll have a look at arrays...Much appreciated.
Janitor
Just out of curiousity: Did you mark this answer with the tick? It was ticked yesterday and now it's not. It is interesting because unticking is not possible as far as I know.
MartyIX
A: 

Hi - some of the options (size and payment method) seem mutually exclusive, so you'd be best placed making these RadioButtons in two separate GroupBoxes. I'd also suggest not performing any calculations in the updates, and instead having a CalculateCost Sub that all of the events defer to when the cost needs recalculating. You should avoid defining e.g. ExtraLargePizza as a String - you could just make this an Integer (or Decimal) - you then won't need to wrap it in Val(...) when performing your calculations.

Will A
A: 

You seem to have a lot of repetition in your code file. You might want to consider using a factory pattern to make it a little easier to create the different kinds of pizza's that you are creating.

Good luck, and hope this helps some.

Chris
+5  A: 

Some principals of VB.NET:

  • Always turn Option Strict on (I've seen horrors because of that)
  • You can directly assign values to variables like this: Dim s As String = "MyString"
  • Do yourself a favor and drop the Microsoft.VisualBasic-Namespace from your project and start using the new classes (like MessageBox instead of MsgBox).*

To your project: A better way of refactoring might would be to simply stuff everything into a Dictionary(Of String, Decimal), that might work better.

Private _prices As New Dictionary(Of String, Decimal)
Private _balance As Decimal = 0D

' Add this to the constructor '
_prices.Add("Ham", 1D)
_prices.Add("Banana", 1D)
_prices.Add("Pineapple", 1D)
' etc. ... '

For Each item As KeyValuePair(Of String, Decimal) In _prices
    CheckedListBox1.Add(item.Key)
Next

' End of Constructor '

Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
    If e.NewValue Then
        _balance += _prices(CheckedListBox1.Item(e.Index))
    Else
        _balance -= _prices(CheckedListBox1.Item(e.Index))
    End if

    Label4.Text = _balance.ToString("C2")
End Sub

*: As Konrad Rudolph pointed out, this might be a good solution for COnsole-Projects, if you don't want to include the Forms-Namespace. Please see the comments for additional details. Thx.

Bobby
I don’t agree with `MsgBox` vs. `MessageBox` (in particular since the latter requires a reference to `Microsoft.Windows.Forms` which isn’t included by default in console projects. But the rest is spot-on.
Konrad Rudolph
@Konrad Rudolph: Mh, true. But I think this is a matter of personal taste. I'd rather include the `Forms`-Namespace then the legacy VB6 one. Also, I'd never use a MessageBox in a Console-Application...but that might be just me.
Bobby
@Bobby: I have to kind of agree on both counts. The `MsgBox` in console applications I sometimes use for debugging (e.g. when I don’t want to disturb the normal console output flow …).
Konrad Rudolph
@Konrad Rudoplh: Wouldn't the `System.Diagnostics`-Namespace (with it's `Debug` and `Trace`-Class) better suited for that kind? Especially because `Debug`-Commands won't make it into the Release-Build, and will only be there in Debug-Mode.
Bobby