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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HainKurtSr. System AnalystCommented:
nice, but what is the problem?
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.
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
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

HainKurtSr. System AnalystCommented:
maybe you should move line 10-11 to line 17
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

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

total = subtotal + discount
-->
total = subtotal - discount
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

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

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

try

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

IchigoMDAuthor Commented:
Line 20 is right before

 total = subtotal - discount
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
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
IchigoMDAuthor Commented:
Thanks for your help and walking me through this.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.