Lesson 4
Home Up Lesson 1 Lesson 2 Lesson 3 Lesson 4 Lesson 5

ASCII table

Assignment 4

Use of Memory

The programs you have created so far do not do many useful things. Just clicking buttons and changing properties are not useful. The computer is very good at storing information so operations can be performed.

Variables

The term we use to contain values in memory is Variables. Variables are locations in memory. Only one value may be contained within one memory variable. There are many different types of data. There are numbers, letters, words, and combination of letters and numbers. There are also different kinds of numbers, small integers, large integers, decimal numbers. The computer needs to know what type of data is going to be stored. Therefore we need to also set up memory to store each type of information in a way that it can be retrieved easily. Therefore when we give a memory location a name, we also must declare what type of information is going to be stored. When we declare the type of variable, the computer reserves enough memory space for that particular type. This is basically the same as we do when we set fields to store data.

Variable Types

Type Value Range General Type Memory Size Prefix
Byte 0..255 integer 1 byte byt
Integer -32767..32767 integer 2 bytes int
Long -2,147,483,648...2,147,483,647 integer 4 bytes lng
Single -3.4E+38 to -1.4E-45 for negatives...1.4E-45 to 3.4E+38 for positives real 4 bytes sng
Double -1.8E+308 to -4.9E-324 for negative values 4.9E-324 to 1.8E+308 for positive values real 8 bytes dbl
Currency -922,337,203,685,477.5808 to 922,337,203,685,477.5807 real 8 bytes cur
String Any characters up to 2 billion characters String Each character takes on byte + 10 bytes str
Date January 1, 100 to December 31, 9999 and time values Date 8 bytes dte
Boolean True,False Logical 2 bytes bln

Creating Variable Names

Decide on what type to use for the data you want to store. Then look at the table above for the corresponding Prefix. Then choose a descriptive name for the variable, based on what data is going to be stored.

Example

To store some one's First Name. It would be a String Type, so the prefix would be str.

strFirstName, would be an appropriate variable name.

To store someone's Age. It would be a Byte Type, so the prefix would be byt.

bytAge, would be an appropriate variable name.

Declaring Variables

Now the magic words that prepare the computer to assign a block of memory with the name you want to give the variable. Position the insertion point after the Private Sub... line. Type Dim, a space, the name you have given the variable, As, a space, the type of the variable, and lastly a comment that explains what the variable will store. For example, to declare the variables above you would include declarations of:

Dim strFirstName As String              'Employees First name
Dim bytAge As Byte                        'Employees age

Now you have two variables declared and you can now store information in the computers memory and use the data stored there.

Assigning Variables some values

The DIM statements have now prepared memory to store data in memory locations with the names that you have given them. Now you will learn how to assign the variables values to store. The Assignment statement is the magic way of putting a value into a variable. Begin with the variable name to assign, an equal sign, followed by the expression to store. For example to put a 17 into the variable bytAge, the following assignment statement would do this.

bytAge = 17

To store my name in the variable, strFirstName, the assignment statement would look like this.

strFirstName = "Dale Coleman"

Notice that my name has quotes enclosing it. It is necessary to enclose certain types of data within symbols so the computer knows the data is of the correct type. Use the following chart to enclose data properly.

Enclosure Symbols for various Data Types
Data Type Symbols Example
Numeric no symbols needed -34.67
String quotes " " "What a Wonderful Life"
Date pound signs  #  # #02/10/99#
Boolean no symbols needed True or False

*Note* - The Boolean data type can only store true or false

You may also store data from properties of objects, for example if the user enters their name into a text box named txtName you may store that data in a string variable called strFullName by the following statement:

strFullName = txtName.Text

Mathematical expressions

The computer loves to do math! Actually that is all it can do! The mathematical operators are the symbols used to perform the various mathematical functions.

VBA Mathematical operators
Operator Meaning
+ Addition
- Subtraction
* Multiplication
/ Division
\ Integer division
Mod Modulus
^ Exponentiation

Storing the calculation in a variable is done with the use of an assignment statement. Use a variable on the left side of the equal sign and the mathematical expression on the right side. The computer will evaluate the expression and store the result in the variable.

Dim sngAnswer As Single
sngAnswer = 5 + 4 *(3-2/1)

The code above will declare a single type variable. And store the value 9.0 in the variable sngAnswer. Notice that the result is a decimal value, this is because of the division operation that always results in a decimal.

Visual Basic also follows the order of operations; with Exponentiation executed first; Multiplication, Division, Integer division and Modulus executed second; Addition and subtraction executed last. Parenthesis alter the order, with the expression deepest in the parenthesis being executed first.

When you want the computer to calculate a value, it is best to declare a variable to store the result of the calculation. You must be careful that the type you specify for the variable matches the result of the calculation. For example: if you declare a variable bytAnswer as a byte type and then use the following assignment statement, bytAnswer = 1/2 then there will be an error. When ever you divide using this operator, you produce a decimal type result. Since bytAnswer is declared to hold an integer type, the computer can not perform the operation.

String Expressions

Visual Basic allows you to perform some amazing operations on strings! However, today we will look at an operation called concatenation. This operation lets you tie strings together. The symbol for concatenation is the ampersand (&) symbol.

For example:

Dim strFullName As String
strFullName = "Dale" & " " & "Coleman"
The string stored in strFullName is Dale Coleman. This statement has concatenated three strings, Dale, a space, and Coleman.

Using Variables in Expressions

Yes Virginia, your work in Algebra has not been a waste of time! The use of variables to calculate values is a fundamental task in most computer programs. This is done exactly the same as using literal values. The most important consideration, is to make sure the variables used in the calculation has a value assigned to it before it is used in the calculation.

For example: (This example assumes there are two text boxes, one named txtLength, the other named txtWidth on the form, and a label named lblArea. And assumes there are valid values in the textboxes)

Dim sngLength As Single
Dim sngWidth As Single
Dim sngArea As Single


txtLength.SetFocus
sngLength = txtLength.Text
txtWidth.SetFocus
sngWidth = txtWidth.Text
sngArea = sngLenth * sngWidth

lblArea.Caption = "The area of the rectangle is: " & sngArea

Explaining the code above: The first three lines declare 3 single type variables in the three dim statements. The 4th through 8th lines stores the values in the two text boxes in variables. The 9th line multiplies the values in the variables together and stores the result in the variable sngArea. The last line concatenates a string with the value stored in the variable. Notice that there are no quotes enclosing the variable sngArea. Visual Basic looks after converting the value stored in sngArea into a string, so you do not get a type mismatch error.

Assignment