Now it is time to learn about some of the fundamentals of programming. Specifically variables. A variable is a declaration that you use in a program to store and retrieve data. For example: X = 5. The variable X stores the value 5. Variables can store data right in the coding, or can store data during runtime. To use a variable, you must first declare it using the Dim statement. Dim means dimension. This becomes more meaningful when you begin to use arrays.
Now it is time to design our interface. We will create a program called Joe Shmoe's Pizza Parlor. Design the interface to appear as above
Name the objects to correspond with the image above.
cmd8inch
cmd12inch
cmdClear
cmdExit
frmPizza
lblCost(0) - Recall the number in the parenthesis requires that you set an index with the objects Index property
lblCost(1) - This will be the smaller label on the image above to the right of the Cost label
lblPizzaQuantity
lblTitle
txtPizzas
txtSpend
lblChange(0)
lblChange(1)
Don't forget to set the access keys as seen in the image. AutoSize the labels.
Lock the form's controls.
Recall: Format > Lock Controls.
OR
Right-click on the form and select Lock Controls.
Open the code window for the cmdClear object.
Type the code to clear the two text boxes and the two labels which display the change and total cost.
txtPizzas.Text = ""
txtSpend.Text = ""
lblCost(1).Caption = ""
lblChange(1).Caption = ""
Type End in the code window for the command button cmdExit.
With the code window still open, select (General) in the code window's object box.
Type Option Explicit
Type Dim sngCash As Single
Press enter
Type Dim sngChange As Single
Press enter
Type Dim sngEightInch as Single
Press enter
Type Dim sngTwelveInch as Single
Press enter
You just declared four variables as Singles. This means that they will store floating-point numbers (decimals) in them. The first line, Option Explicit, tells Visual Basic to generate an error if the code uses an undeclared variable. Undeclared variables are set to the Variant data-type, which uses the most amount of allocated memory. With small programs like the one we are writing, it is no big deal to use an undeclared variable, but it is poor coding using undeclared variables. What you declare a variable as is called a data-type. These variables are called form-level variables, which means that they can be accessed anywhere within the form, but not throughout the project. You can also declare local variables, which can only be accessed in the sub-procedure which it was declared in. The highest level variables you can create are global variables, which are declared in a code module's General Declarations section. You probably noticed a tremendous list that popped up after you typed Dim. This list shows all the possible data-types you can declare a variable as. Since a variable stores data, it needs to store it in memory. Certain data-types use more than others, and some have limits. Here is a list of a few:
Type | Stores | Memory Required | Range of Values |
Byte | Binary numbers | 1 byte | 0 to 255 |
Boolean | Logical values | 2 bytes | True or False |
Currency | Numbers with up to 15 digits to the left of the decimal and 4 digits to the right of the decimal | 8 bytes | +/- 9E14 |
Date | Date and time information | 8 bytes | January 1, 100 to December 31, 9999 |
Double | Floating-point numbers | 8 bytes | +/-5E-324 to 1.8E308 |
Integer | Integers | 2 bytes | -32,768 to 32,767 |
Long | Integers | 4 bytes | +/- 2 billion |
Object | Any object reference | 4 bytes | N/A |
Single | Floating-point numbers | 4 bytes | +/- 1E-45 to 3E38 |
String | Any text | Fixed-length: 1 byte per character Variable-length: 10 bytes + 1 byte per character |
Fixed-length 1 to 65,000 Variable-length: 0 to 2 billion |
Variant | Any of the other data-types | With numbers: 16 bytes With characters: 22 bytes + 1 byte per character |
With numbers: Same as Double With characters: Same range as for variable-length string |
Information above thanks to Diane Zak: Visual Basic 6.0 Enhanced Edition |
Many of these data-types, such as byte, are left over from Command-Line Basic when memory was precious, expensive, and limited. Now we do not have to worry so much about how much memory a variable needs allocated. Now that you know about variables a bit more, I'll continue.
Double-click the command button cmd8inch
Type the following lines of code:
sngCash = Val(txtSpend.Text) 'Store the value of the text entered into txtPizzas
in the variable sngCash
sngEightInch = Val(txtPizzas.Text) * 4.95 'Stores the value of
txtPizzas.Text times 4.95 in the variable sngEightInch
sngChange = sngCash - sngEightInch 'Takes the total cash entered (sngCash)
and subtracts the total value of sngEightInch, then stores that value
in the variable sngChange
lblCost(1).Caption = Format(sngEightInch, "Currency") 'Formats the resulting
variable sngEightInch as currency
Now select cmd12inch in the object box, then enter the following code:
sngCash = Val(txtSpend.Text) 'Store the value of the text entered into txtPizzas
in the variable sngCash
sngTwelveInch = Val(txtPizzas.Text) * 6.95 'Stores the value of
txtPizzas.Text times 6.95 in the variable sngTwelveInch(this is the computation for the total price of the
entire order for the twelve-inch pizza; basically, Quantity{Val(txtPizzas.Text)}
* Price{6.95}) Same as in the Eight-Inch calculation above.
sngChange = sngCash - sngTwelveInch 'Takes the total cash entered (sngCash)
and subtracts the total value of sngTwelveInch, which is stored in
the variable sngChange.
lblCost(1).Caption = Format(sngTwelveInch, "Currency") 'Formats the resulting
variable sngTwelveInch as currency
**Try running the program now. Enter values into the text box and watch the difference in the values.
Remember to refer to the comments I provided if you are confused.
Keep familiar on how to use variables. They will become much more important if you continue through this tutorial.
Well, now it is time to learn about conditional statements on Page 9.