VB.net Program

I am having trouble with a VB.net program. i amtrying to add 5% discount to the subtotal and have the discount show up in the discount box and then have it add to the subtotal to get the toatl and reflect in the total box.
Public Class GroceryStore


    Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
        'Calculate totals
        Dim subtotal As Double
        Dim discount As Double
        Dim total As Double

        discount = 0.05 * subtotal
        total = subtotal + discount
        subtotal = Double.Parse(BananaBox.Text) * 0.44
        subtotal += Double.Parse(ApplesBox.Text) * 0.99
        subtotal += Double.Parse(CucumberBox.Text) * 1.19
        subtotal += Double.Parse(CarrotsBox.Text) * 0.89
        subtotal += Double.Parse(OrangesBox.Text) * 0.79

        SubtotalBox.Text = subtotal.ToString("C2")

        If subtotal >= 50.0 Then discount = 0.05 * Double.Parse(SubtotalBox.Text)
        Else discount = 0

        DiscountBox.Text = discount.ToString("C2")


    End Sub

    Private Sub GroceryStore_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
        'Close the window and exit the application.
        Me.Close()

    End Sub

    Private Sub BananaBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BananaBox.TextChanged

    End Sub

    Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
        'Clears the fields and sets the focus on the Banana box so user can start over.
        Me.BananaBox.Clear()
        Me.ApplesBox.Clear()
        Me.CucumberBox.Clear()
        Me.CarrotsBox.Clear()
        Me.OrangesBox.Clear()
        Me.SubtotalBox.Text = ""
        Me.DiscountBox.Text = ""
        Me.BananaBox.Focus()
    End Sub
End Class

Open in new window

IchigoMDAsked:
Who is Participating?
 
IchigoMDAuthor Commented:
Got it to work . My mistake was I didn't have the code to input the text in the total box. Thanks a bunch for walking me through this. I can't believe i worked it out. Thanks.
0
 
HainKurtSr. System AnalystCommented:
nice, but what is the problem?
0
 
IchigoMDAuthor Commented:
I keep getting error stating the structure is not correct on the discount portion of the code. I think I am leaving something out i just don't know what.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
HainKurtSr. System AnalystCommented:
look at this code:


'Calculate totals
Dim subtotal As Double
Dim discount As Double
Dim total As Double

discount = 0.05 * subtotal >>>> subtotal is not initialized, or maybe value is 0, so discount will be 0
total = subtotal + discount >>>> discount is zero, subtotal is also zero, not assigned
0
 
HainKurtSr. System AnalystCommented:
maybe you should move line 10-11 to line 17
0
 
HainKurtSr. System AnalystCommented:
try this
Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
        'Calculate totals
        Dim subtotal As Double = 0
        Dim discount As Double = 0
        Dim total As Double = 0

        subtotal = Double.Parse(BananaBox.Text) * 0.44
        subtotal += Double.Parse(ApplesBox.Text) * 0.99
        subtotal += Double.Parse(CucumberBox.Text) * 1.19
        subtotal += Double.Parse(CarrotsBox.Text) * 0.89
        subtotal += Double.Parse(OrangesBox.Text) * 0.79

        SubtotalBox.Text = subtotal.ToString("C2")

        If subtotal >= 50.0 Then discount = 0.05 * subtotal
        Else discount = 0

        total = subtotal + discount

        DiscountBox.Text = discount.ToString("C2")
    End Sub

Open in new window

0
 
IchigoMDAuthor Commented:
Tried that but it states there is a problem when compiling
0
 
HainKurtSr. System AnalystCommented:
oops, I guess your logic should be corrected as

total = subtotal + discount
-->
total = subtotal - discount
0
 
IchigoMDAuthor Commented:
I keep getting there are build errors I attached the code again.


Public Class GroceryStore


    Private Sub CalculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculateButton.Click
        'Calculate totals
        Dim subtotal As Double = 0
        Dim discount As Double = 0
        Dim total As Double = 0

        subtotal = Double.Parse(BananaBox.Text) * 0.44
        subtotal += Double.Parse(ApplesBox.Text) * 0.99
        subtotal += Double.Parse(CucumberBox.Text) * 1.19
        subtotal += Double.Parse(CarrotsBox.Text) * 0.89
        subtotal += Double.Parse(OrangesBox.Text) * 0.79

        SubtotalBox.Text = subtotal.ToString("C2")

        If subtotal >= 50.0 Then discount = 0.05 * subtotal
        Else discount = 0

        total = subtotal - discount

        DiscountBox.Text = discount.ToString("C2")


    End Sub

    Private Sub GroceryStore_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
        'Close the window and exit the application.
        Me.Close()

    End Sub

    Private Sub BananaBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BananaBox.TextChanged

    End Sub

    Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
        'Clears the fields and sets the focus on the Banana box so user can start over.
        Me.BananaBox.Clear()
        Me.ApplesBox.Clear()
        Me.CucumberBox.Clear()
        Me.CarrotsBox.Clear()
        Me.OrangesBox.Clear()
        Me.SubtotalBox.Text = ""
        Me.DiscountBox.Text = ""
        Me.BananaBox.Focus()
    End Sub
End Class

Open in new window

0
 
HainKurtSr. System AnalystCommented:
where do you get that error and what is the exact messages
0
 
IchigoMDAuthor Commented:
The error is: There is no source code available. On line 20 it says input string is not in correct format
0
 
HainKurtSr. System AnalystCommented:
is Line 20 this one?

DiscountBox.Text = discount.ToString("C2")

try

DiscountBox.Text = discount.ToString("C")
0
 
IchigoMDAuthor Commented:
Still getting the same error.
0
 
HainKurtSr. System AnalystCommented:
same error on which line?

0
 
IchigoMDAuthor Commented:
Line 20 is right before

 total = subtotal - discount
0
 
HainKurtSr. System AnalystCommented:
I don't see any issue here which will give that error...

        If subtotal >= 50.0 Then discount = 0.05 * subtotal
        Else discount = 0

        total = subtotal - discount

put end if...


        If subtotal >= 50.0 Then
           discount = 0.05 * subtotal
        Else
           discount = 0
        End If

        total = subtotal - discount
0
 
IchigoMDAuthor Commented:
Thanks for your help and walking me through this.
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.