[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB.net Program

Posted on 2011-10-03
17
Medium Priority
?
204 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:IchigoMD
  • 9
  • 8
17 Comments
 
LVL 61

Expert Comment

by:HainKurt
ID: 36907490
nice, but what is the problem?
0
 

Author Comment

by:IchigoMD
ID: 36907502
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
 
LVL 61

Expert Comment

by:HainKurt
ID: 36907504
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 61

Expert Comment

by:HainKurt
ID: 36907509
maybe you should move line 10-11 to line 17
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 36907515
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
 

Author Comment

by:IchigoMD
ID: 36907518
Tried that but it states there is a problem when compiling
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 36907520
oops, I guess your logic should be corrected as

total = subtotal + discount
-->
total = subtotal - discount
0
 

Author Comment

by:IchigoMD
ID: 36907537
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
 
LVL 61

Expert Comment

by:HainKurt
ID: 36907546
where do you get that error and what is the exact messages
0
 

Author Comment

by:IchigoMD
ID: 36907558
The error is: There is no source code available. On line 20 it says input string is not in correct format
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 36907576
is Line 20 this one?

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

try

DiscountBox.Text = discount.ToString("C")
0
 

Author Comment

by:IchigoMD
ID: 36907583
Still getting the same error.
0
 
LVL 61

Expert Comment

by:HainKurt
ID: 36907591
same error on which line?

0
 

Author Comment

by:IchigoMD
ID: 36907592
Line 20 is right before

 total = subtotal - discount
0
 
LVL 61

Assisted Solution

by:HainKurt
HainKurt earned 2000 total points
ID: 36907621
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
 

Accepted Solution

by:
IchigoMD earned 0 total points
ID: 36907642
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
 

Author Closing Comment

by:IchigoMD
ID: 36938090
Thanks for your help and walking me through this.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question