Link to home
Start Free TrialLog in
Avatar of IchigoMD
IchigoMD

asked on

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

Avatar of HainKurt
HainKurt
Flag of Canada image

nice, but what is the problem?
Avatar of IchigoMD
IchigoMD

ASKER

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

Tried that but it states there is a problem when compiling
oops, I guess your logic should be corrected as

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

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

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

try

DiscountBox.Text = discount.ToString("C")
Still getting the same error.
same error on which line?

Line 20 is right before

 total = subtotal - discount
SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your help and walking me through this.