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

.NET Programming

Avatar of undefined
Last Comment
IchigoMD
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.
Avatar of HainKurt
HainKurt
Flag of Canada image

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
Avatar of HainKurt
HainKurt
Flag of Canada image

maybe you should move line 10-11 to line 17
Avatar of HainKurt
HainKurt
Flag of Canada image

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

Avatar of IchigoMD
IchigoMD

ASKER

Tried that but it states there is a problem when compiling
Avatar of HainKurt
HainKurt
Flag of Canada image

oops, I guess your logic should be corrected as

total = subtotal + discount
-->
total = subtotal - discount
Avatar of IchigoMD
IchigoMD

ASKER

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

Avatar of HainKurt
HainKurt
Flag of Canada image

where do you get that error and what is the exact messages
Avatar of IchigoMD
IchigoMD

ASKER

The error is: There is no source code available. On line 20 it says input string is not in correct format
Avatar of HainKurt
HainKurt
Flag of Canada image

is Line 20 this one?

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

try

DiscountBox.Text = discount.ToString("C")
Avatar of IchigoMD
IchigoMD

ASKER

Still getting the same error.
Avatar of HainKurt
HainKurt
Flag of Canada image

same error on which line?

Avatar of IchigoMD
IchigoMD

ASKER

Line 20 is right before

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of IchigoMD
IchigoMD

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of IchigoMD
IchigoMD

ASKER

Thanks for your help and walking me through this.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo