Results 1 to 6 of 6

Thread: Bagel and Coffee Price Calculator Help VB 2010

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2013
    Posts
    2

    Bagel and Coffee Price Calculator Help VB 2010

    Here is my code:

    Public Class Form1
    ' Class - level declarations
    Const decTAX_RATE As Decimal = 0.06D ' Tax Rate
    Const decWHITE_BAGEL As Decimal = 1.25D ' Cost of a white bagel
    Const decWHEAT_BAGEL As Decimal = 1.5D ' Cost of a whole wheat bagel
    Const decCREAM_CHEESE As Decimal = 0.5D ' Cost of cream cheese topping
    Const decBUTTER As Decimal = 0.25D ' Cost of butter topping
    Const decBLUEBERRY As Decimal = 0.75D ' Cost of blueberry topping
    Const decRASPBERRY As Decimal = 0.75D ' Cost of raspberry topping
    Const decPEACH As Decimal = 0.75D ' Cost of peach topping
    Const decREG_COFFEE As Decimal = 1.25D ' Cost of regular coffee
    Const decCAPPUCCINO As Decimal = 2D ' Cost of cappuccino
    Const decCAFE_AU_LAIT As Decimal = 1.75D ' Cost of cafe au lait

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    ' This procedure calculates the total of an order.
    Dim decSubtotal As Decimal ' holds the order subtotal
    Dim decTax As Decimal ' holds the sales tax
    Dim decTotal As Decimal ' holds the order total

    decSubtotal = BagelCost() + ToppingCost() + CoffeeCost()
    decTax = CalcTax(decSubtotal)
    decTotal = decSubtotal + decTax

    lblSubtotal.Text = decSubtotal.ToString("c")
    lblTax.Text = decTax.ToString("c")
    lblTotal.Text = lblTotal.ToString("c")
    End Sub

    Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
    ' This procedure resets the controls to default values.
    ResetBagels()
    ResetToppings()
    ResetCoffee()
    ResetPrice()
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    ' Close the form
    Me.Close()
    End Sub
    End Class
    Function BagelCost() As Decimal
    ' This function returns the cost of the selected bagel.
    Dim decBagel As Decimal

    If radWhite.Checked = True Then
    decBagel = decWHITE_BAGEL
    Else
    decBagel = decWHEAT_BAGEL
    End If

    Return decBagel
    End Function

    Function ToppingCost() As Decimal
    ' This function returns the cost of the toppings.
    Dim decCostOfTopping As Decimal = 0D

    If chkCreamCheese.Checked = True Then
    decCostOfTopping += decCREAM_CHEESE
    End If

    If chkButter.Checked = True Then
    decCostOfTopping += decBUTTER
    End If

    If chkBlueberry.Checked = True Then
    decCostOfTopping += decBLUEBERRY
    End If

    If chkRaspberry.Checked = True Then
    decCostOfTopping += decRASPBERRY
    End If

    If chkPeach.Checked = True Then
    decCostOfTopping += decPEACH
    End If

    Return decCostOfTopping
    End Function

    Function CoffeeCost() As Decimal
    ' This function returns the cost of the selected coffee.
    Dim decCoffee As Decimal

    If radNoCoffee.Checked Then
    decCoffee = 0D
    ElseIf radRegCoffee.Checked = True Then
    decCoffee = decREG_COFFEE
    ElseIf radCappuccino.Checked = True Then
    decCoffee = decCAPPUCCINO
    ElseIf radCafeAuLait.Checked = True Then
    decCoffee = decCAFE_AU_LAIT
    End If

    Return decCoffee
    End Function

    Function CalcTax(ByVal decAmount As Decimal) As Decimal
    ' This function recieves the sale amount and
    ' returns the amount of sales tax.
    Return decAmount * decTAX_RATE
    End Function

    Sub ResetBagels()
    ' This procedure resets the bagel selection.
    radWhite.Checked = True
    End Sub

    Sub ResetToppings()
    ' This procedure resets the topping selection.
    chkCreamCheese.Checked = False
    chkButter.Checked = False
    chkBlueberry.Checked = False
    chkRaspberry.Checked = False
    chkPeach.Checked = False
    End Sub

    Sub ResetCoffee()
    ' This procedure resets the coffee selection.
    radRegCoffee.Checked = True
    End Sub

    Sub ResetPrice()
    ' This procedure resets the price.
    lblSubtotal.Text = String.Empty
    lblTax.Text = String.Empty
    lblTotal.Text = String.Empty
    End Sub

    --------------------------------------------------------------------
    When I debug it I get 16 errors. The first 8 are because my BagelCost, ToppingCost, CoffeeCost, CalcTax, ResetBagels, ResetToppings, ResetCoffee, and ResetPrice are not declared. The rest of my errors have to do with Function and Sub being ' statement is not valid in a namespace'.

    This is an example from by textbook and in the end it says that if there are errors, use debugging techniques you have learned to find them and correct them. So I'm guessing I have to use the step into/ step over / step out techniques. But even if i try that nothing changes, but I might be doing it the wrong way.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Bagel and Coffee Price Calculator Help VB 2010

    The first 8 are because my BagelCost, ToppingCost, CoffeeCost, CalcTax, ResetBagels, ResetToppings, ResetCoffee, and ResetPrice are not declared.
    So declare them!

    The rest of my errors have to do with Function and Sub being ' statement is not valid in a namespace'.
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    ' Close the form
    Me.Close()
    End Sub
    End Class <<<<<<< This line should be at the end of the program, not here!
    Function BagelCost() As Decimal
    ' This function returns the cost of the selected bagel.
    Dim decBagel As Decimal

    But even if i try that nothing changes
    The program can't compile so these methods are irrelevant. If the program doesn't run, it doesn't run.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2013
    Posts
    2

    Re: Bagel and Coffee Price Calculator Help VB 2010

    Quote Originally Posted by dunfiddlin View Post
    So declare them!



    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    ' Close the form
    Me.Close()
    End Sub
    End Class <<<<<<< This line should be at the end of the program, not here!
    Function BagelCost() As Decimal
    ' This function returns the cost of the selected bagel.
    Dim decBagel As Decimal



    The program can't compile so these methods are irrelevant. If the program doesn't run, it doesn't run.
    Thank You! Moving That 'End Class' To the bottom solved all the errors! :'D
    Now when I debug it and test it out and click on the calculate total button to test if it works or not I get an error " Additional information: Conversion from string "c" to type 'Integer' is not valid. " and it highlights this part from my code : lblTotal.Text = lblTotal.ToString("c")

    Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    ' This procedure calculates the total of an order.
    Dim decSubtotal As Decimal ' holds the order subtotal
    Dim decTax As Decimal ' holds the sales tax
    Dim decTotal As Decimal ' holds the order total

    decSubtotal = BagelCost() + ToppingCost() + CoffeeCost()
    decTax = CalcTax(decSubtotal)
    decTotal = decSubtotal + decTax

    lblSubtotal.Text = decSubtotal.ToString("c")
    lblTax.Text = decTax.ToString("c")
    lblTotal.Text = lblTotal.ToString("c")
    End Sub

    I don't understand why only that line is highlighted because the other two lined prior to that isn't and it .

  4. #4
    New Member
    Join Date
    Aug 2013
    Posts
    10

    Re: Bagel and Coffee Price Calculator Help VB 2010

    Quote Originally Posted by fluffyxcake View Post
    lblSubtotal.Text = decSubtotal.ToString("c")
    lblTax.Text = decTax.ToString("c")
    lblTotal.Text = lblTotal.ToString("c")

    I don't understand why only that line is highlighted because the other two lined prior to that isn't and it .
    That would be because for the previous two lines you say 'labelX.text = decY.tostring("c") while in the final one you do lblTotal.tostring("c").

    If i understand correctly it should be;
    Code:
    lbltotal.Text = decTotal.ToString("c")

  5. #5
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Bagel and Coffee Price Calculator Help VB 2010

    Quote Originally Posted by fluffyxcake View Post
    lblSubtotal.Text = decSubtotal.ToString("c")
    lblTax.Text = decTax.ToString("c")
    lblTotal.Text = lblTotal.ToString("c")
    End Sub

    I don't understand why only that line is highlighted because the other two lined prior to that isn't and it .
    Agreed, it is very strange. Maybe take a closer look?

  6. #6
    Member
    Join Date
    Aug 2012
    Location
    Nottingham, UK
    Posts
    44

    Re: Bagel and Coffee Price Calculator Help VB 2010

    JimmyB is correct, that line should be
    Code:
    lblTotal.Text = decTotal.ToString("c")
    ~ TheMeq ~
    If I helped in anyway, please hit the star and send me some rep! Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width