food menu
food menu
food menu
food menu
food menu
food menu food menu food menu food menu food menu food menu food menu food menu
food menu food menu
food menu
Go Back  Xtreme Visual Basic Talk > > > food menu


Reply
 
Thread Tools Display Modes
  #1  
Old 10-22-2013, 03:41 PM
Pyromark Pyromark is offline
Newcomer
 
Join Date: Oct 2013
Posts: 9
Default food menu


Im making a food menu program for class. i have 3 buttons for the sizes of pizza. When i click small. label on the right comes up with 1 and then puts 6.00 in subtotal box. when i click medium, it doesn't add to the 6.00 it just puts its own price as 9.00. how do i add them all together.
Public Class Form1

Code:
Private Sub btnSmall_Click(sender As Object, e As EventArgs) Handles btnSmall.Click lblSmall.Text = Val(lblSmall.Text) + 1 Dim declblSubtotal As Integer declblSubtotal = Val(lblSmall.Text) * 6 lblSubtotal.Text = declblSubtotal.ToString("c") End Sub Private Sub btnMedium_Click(sender As Object, e As EventArgs) Handles btnMedium.Click lblMedium.Text = Val(lblMedium.Text) + 1 Dim declblSubtotal As Integer declblSubtotal = Val(lblMedium.Text) * 9 lblSubtotal.Text = declblSubtotal.ToString("c") End Sub

Last edited by Gruff; 10-22-2013 at 05:42 PM.
Reply With Quote
  #2  
Old 10-22-2013, 05:08 PM
Icehawke Icehawke is offline
Newcomer
 
Join Date: Aug 2013
Location: Elgin IL
Posts: 17
Default

You are resetting the value of declblSubtotal each time you click the buttons. You need to widen the scope of that variable.
Reply With Quote
  #3  
Old 10-22-2013, 05:23 PM
Pyromark Pyromark is offline
Newcomer
 
Join Date: Oct 2013
Posts: 9
Default

how so?
Reply With Quote
  #4  
Old 10-22-2013, 05:55 PM
Gruff's Avatar
Grufffood menu Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

If you read up on scope you will see that among other things it controls
where variables can be seen and affected by code.

Variables created inside subroutines are created and destroyed all within the sub routine.

Code:
Dim A as integer = 6 '(Variable defined before any sub routines are global to the form) and exist until the form is closed. Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click A += 12 'A now equals 18 'A += 12 is equivalent to saying A = A + 12 End Sub Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click A += 22 'A now equals 40 End Sub
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 10-22-2013 at 06:00 PM.
Reply With Quote
  #5  
Old 10-22-2013, 05:59 PM
Pyromark Pyromark is offline
Newcomer
 
Join Date: Oct 2013
Posts: 9
Default

thanks for the reply but does that make it so everytime i click the button it keeps adding whatever the price is?
Reply With Quote
  #6  
Old 10-23-2013, 08:49 AM
Icehawke Icehawke is offline
Newcomer
 
Join Date: Aug 2013
Location: Elgin IL
Posts: 17
Default

If you click btn1 once, A is 18. If you click it a second time, A becomes 30. A third time and 42. And so on.
Reply With Quote
  #7  
Old 10-23-2013, 10:13 AM
Gruff's Avatar
Grufffood menu Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Yes,

The value of A will be increased each time you click a button.
The total will be which buttons you pressed and how many times.

The idea here is that you use this knowledge to fix your original code where A is your declblSubtotal variable.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #8  
Old 10-23-2013, 11:13 AM
Pyromark Pyromark is offline
Newcomer
 
Join Date: Oct 2013
Posts: 9
Default

ohhhh i understand now. but do i have to fix it so EACH code is like that? bc i have 3 buttons. i have it down to were when i click small 3 times, yes it will add up but when i click medium, it will automatically start over at 9$ for medium when i want it to actually add to the existing price
Reply With Quote
  #9  
Old 10-23-2013, 11:14 AM
Pyromark Pyromark is offline
Newcomer
 
Join Date: Oct 2013
Posts: 9
Default

i understand what ur saying but can you get me started on how to actually change it?
Reply With Quote
  #10  
Old 10-23-2013, 11:31 AM
AtmaWeapon's Avatar
AtmaWeaponfood menu AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Think about what you are doing mathematically.

The subtotal should be:
subtotal = ax + by + cz

Right now your code either uses:
subtotal = ax
-or-
subtotal = by

How could you change it so every time you have the correct subtotal?
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #11  
Old 10-23-2013, 11:46 AM
Pyromark Pyromark is offline
Newcomer
 
Join Date: Oct 2013
Posts: 9
Default

I need it so subtotal = ax + bx +cx
Reply With Quote
  #12  
Old 10-23-2013, 11:47 AM
Pyromark Pyromark is offline
Newcomer
 
Join Date: Oct 2013
Posts: 9
Default

ax + by + cz * u know what i mean i have it so each button it puting the correct price in the subtotal box, but idk where to put or how to put the code to add them all up
Reply With Quote
  #13  
Old 10-23-2013, 12:24 PM
Gruff's Avatar
Grufffood menu Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

You were given more than enough information in this thread to solve your problem.
It is not our job to write your code for you.

Remove: Dim declblSubtotal As Integer (From each button_click code event)
Add: one copy of it before all of your button_click events. (See Post #4 defining A)

Change: declblSubtotal = Val(<Label>.Text) * n
to
declblSubtotal += Val(<Label>.Text) * n (In each button_click event)

Notice the += operator in the corrected code. += adds a value to the existing variable value
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
food menu
food menu
food menu food menu
food menu
food menu
food menu food menu food menu food menu food menu food menu food menu
food menu
food menu
 
food menu
food menu
 
-->