You are on page 1of 7

Visual Basic Code—Part 1 Variables

Lesson

In previous lessons you learned that variables are containers that programming
languages use to store information.  In examples we have used variables to store the
result of simple calculations in the computer's memory.  Now we will look at variables in
more detail.

Variables are used to store both numerical and text information. For example variables
can store your height or your name.  Text information, when stored in variables, is
collectively called a character string or simply a string.  The following are strings:

"John Smith"
"123 Main Street"
"2 + 4"

Note that strings are contained within the double quote " " symbol.  Even numbers can
be strings and are treated the same as other characters. 

Numbers can be stored in a variety of additional formats depending on the type of


number. In math courses you have learned the difference between whole numbers,
integers, decimal or real numbers, and so on.  Visual Basic has variable types for all
these and more.  Here is a table of the categories of variables.
 

Visual Basic Variable Types

  Type Type Description


Symbol
Integer % Short Integers. Whole numbers between  -32,768 and +
32,767

Long & Long Integers. Whole numbers between approximately


-2 and +2 billion

Single ! Single precision decimal number.  Accuracy to 7 digits


and a range between 10-45 and 10+38

Double # Double precision decimal number. Accuracy to 17 digits.


A range between 10-324 and 10+308

Bool   Boolean type.  Can only have two values, true or false (-
1 or 0)

Byte   Integer between 0 and 255.  Can also represent


characters.

Currency @ Currency (money) type. Can have values between


±922,337,203,685,477.50 (say that in words!)

Date   Date type.  Stores the date and time as a number

String $ String type. Sequences of characters not treated as


numbers by Visual Basic

Variant   This is the default type.  Can contain either numbers or


strings.

The type symbol is optional and doesn't need to be used if the variable is declared with
a Dim statement.

Declaring Variable Types

If the variable is not set to a specific type it becomes, by default, a Variant.  If your
application needs to set the type for each variable, this can be done in a number of
ways.

The Dim keyword,  which you have seen, is short for dimension, and is used to set a
variable to a particular type.  For example:

Dim Counter as integer


Dim Flag as Boolean
Dim Counter1 as integer, Counter2 as integer, TotalCost as Currency

Note that in the last example you can declare more than one variable on a line as long
as they are separated by commas and the type is declared each time.

If you used the following syntax,


Dim Sum1, Sum2, Sum3 as integer

only the last variable Sum3 would be set as an integer type.  Sum1 and Sum2 would be
set to the Variant type.

Variable Assignment

A number or character string is "assigned" to the appropriate variable using the equals
(=) sign.  For example

X = 10
SalesTax = 79.56
Dog = "Rover"

are all examples of variable assignment.  They are not equations in the normal sense. 
Think of the assignment as meaning that you are assigning to a variable (storage
location) some contents.  That is, 10 is stored in a variable named X, etc.

Scope of a Variable

The scope of the variable has to do with where it will be recognized.  Local level and
Module level are used to describe the scope of a variable.

 Local Level: Declared in any event or general procedure with the keyword
Dim. These variables only exist within the procedure in which they are
declared. There is no problem with having local variables in many procedures
with the same name. They all will refer to different storage spaces.
 Module Level: Declared in the code window's General Declarations section,
with the keyword Dim. These variables are accessible from any event or
general procedure in the module.

This can be a bit confusing, so let's look at some examples.  We want to display a
decimal number in a text box and since we are talking about variables, we want to
store the number in a variable.  Consider the following:

Figure Local Level Variable Declaration

When you click the Command1 button you might expect to see the number 17.4567
appear in the text box.  It will not and here's why.  When the program is started, the
Form_Load procedure will be executed first.  Here we declare the variable x as single
because we want to store a decimal number.  Then the number is assigned to the
variable.

If you click the Command1 button, the contents of x are displayed in the Text1 box.
However, the Command1_Click procedure knows nothing about the variable x as it is
local to the Form_Load procedure.

In order for the variable to be recognized by both procedures, you must make the
variable a Module level by declaring it in the General Declarations section of the Form. 
Consider the following:

Figure Module level variable declaration

This will work because now all procedures in the module (the form code window) will
recognize and can change the contents of the variable, x.

It should be noted that the keyword Private can be used instead of Dim in the version
of Visual Basic you are using.

Concatenation

That's a mouthful! Consider the following:

Text1 = 2 + 2
Text1 = "2" + "2"

The first line would display the number 4 in the text box.  The computer will evaluate
the expression 2 + 2 and assign the answer 4.  In the second line, the string 22 would
be displayed in the text box.  Note that in the second line, "2" + "2" each number 2 is
enclosed in quotes.

In the first line the + sign means addition.  In the second line the + sign means
concatenation or the joining of strings.  You can concatenate two string expressions as
in this example or two sting variables as in the following example:
Figure Concatenation of two strings

If the Command button is clicked the concatenated string John Dough will be displayed
in the text1 box.  Did you notice that there is a space in the string "John ". If there
wasn't, the sting would display as JohnDough.  Of course you could always place a
space before Dough and it would display properly.

There is another symbol used for concatenation, the & sign.  In the following example,
the two lines will give the same result:

Text1 = "2" + "2"


Text1 = "2" & "2"

Constants

This is a special variable type where the value of the variable does not change.  It is
declared in the General Declarations section. Here is an example:

Const Pi as Single = 3.14159

This might then be used in a familiar calculation such as:

Circumference = Pi * D

One final note.  In many of the programs you will write in future lessons the Variant
variable type is probably sufficient.  In special circumstances you may have to use one
of the specific variable types.

Activity
The purpose of this activity is to build capability with declaring and using variables and
variable types

Activity 1 Questions

Go back and have a look at lesson 2.  We used a variable and declared it in the General
Declarations section.  Explain the following:
 The type of variable
 The scope of the variable

Copy all files to your course portfolio

Activity 2 Create a Program

Create a Visual Basic Program that has one command button and two text boxes. When
you run the program, the user will type a decimal number into text1 (eg. 23.45534)
and the whole number part of the number (eg. 23) will be displayed in text2.

Hints:
In general declarations, declare (dim) a variable (make up a name for the variable) as
one of the variable types found in the chart in lesson 3 (you need to figure out which
one).
In the Command1 event procedure, let the variable = text1, then in the next line, let
text2 = the variable.

Copy all files to your course portfolio

Activity 3 Create a Concatenation Program

Create a Visual program that that has one command button and three text boxes. The
user types a word into each of the first two text boxes and when the command button
is clicked, the two words will appear as a single phrase in the third text box. You are to
use two variables, one for each word and you must declare (dim) each in the general
declarations code area. Oh, by the way, the words should have a space between them.

Hint: Make sure you declare the variables as the right type (you figure out which one)
then use the concept of concatenation to "join" the words, but don't forget the space
(remember you can place characters between double quotes " " and they will be
displayed literally).

Copy all files to your course portfolio

Activity 4 Create a Sales Tax Program

Create a Visual Basic program to calculate and display the Newfoundland and Labrador
sales tax of 15%. 

 Use two text boxes and a command button. 


 Type the price of an item in the first text box (in dollars and cents) and display
the tax in the second text box in the correct format. 
 Make sure you declare (dim) a variable, or variables, of the correct type.

Hint: To give the display an even better look you can use a Visual Basic format
function. Here's how it works.

Text2 = Format (variable name, "currency")


Variable name is the name you gave the variable.  Make sure you use double quotes
around currency.

Copy all files to your course portfolio

You might also like