Visual Basic Programming

Page 8

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

With the code window still open, select (General) in the code window's object box. 

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.

**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.

Top of Page

Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6 | Page 7 | Page 8 | Page 9 | Page 10 | Page 11

Page 12 | Page 13 | Page 14 | Page 15 | Page 16 | Page 17 | Page 18 | Page 19 | Page 20

Page 21 | Page 22 | Page 23 | Page 24 | Page 25 | Page 26 | Page 27 | Page 28 | Page 29 | Page 30