Home -> Programming -> Processing -> Variables

Variables

Variables are places to temporarily store information which will be used later in the program. Just as the CPU of a computer uses RAM to store information temporarily while processing is done, a variable is a program's method of setting aside a part of RAM to hold some particular information which will be needed later. In converting Input into Output (processing), we often need to temporarily store information for later use. Variables are just like a calculator store, or memory feature, except there is much more of it and it is much more flexible than a calculator's memory. That flexibility also means it is necessarily more complex to use.

Two of the issues that come up right away when learning to program are the questions:

  1. When do I need to declare a variable?
  2. The answer to this is given in the first sentence above. When we create data which we will want to use later in our program, but not in our next operation, then we need to declare a variable so we can temporarily store our data in it.

  3. How many variables do I need?
  4. Again here the answer can be had from the first paragraph. When we create some data which we want to use later, we need to declare a variable for each piece of data we create and store that data in the variable created. it is important that we only store one piece of data in each variable. Experience has shown that when programmers try to use a variable for more than one purpose, it always causes problems for them later. If you create a vriable, and then use it or output it immediately, then there was no need to create it in the first place. We only create and use variables to store data which we must have later on in our program.

Variable Declaration

Most programming languages require you to declare a variable before you use it. Visual BASIC can be set to require variable declarations or not. The purpose of variable declarations is so the program can set aside enough RAM to hold the data which is to be stored in the variable. However, if a variable is not declared then VB assumes the worst and sets aside the maximum storage possible. For anything but very small programs this can be a very bad thing. Therefore, you should always declare variables in VB. VB also allows you to declare variables anywhere in your code as long as it is done before the variable is used. This flexibility is nice, but traditionally all variables are declared at the beginning of the code module. After you have spent several minutes hunting for a declaration, you will understand why. I suggest you form two habit in using variables. First, declaring all variables; Second, declare variables at the beginning of their code module.

There are many different data types for variables. It is especially confusing to new programmers. In order to simplify this for you I categorize the variables and then discuss the various types in each category. Variables can be categorized as follows:

Variables have two more key properties which have a major impact on the way in which we can use them. These properties are: The results of these two properties of variables means that when we declare a variable we do it with one of the following keywords. The keyword we use and the place where we make the declaration then determine the scope and lifetime of the variable. The possibilities are outlined in the table below

KeywordScopeLifetimeWhere UsedBehaviour
DimLocalWhile sub/function is runningDeclared immediately after the sub/function declarationVariable is reinitialized to zero every time the procedure runs.
StaticLocalThe duration of the parent object - usually the formDeclared immediately after the sub/function declarationThe value of static variables is preserved. It behaves like it is a module variable, but its scope is local.
PrivateModuleDuration of the formDeclared immediately after the form declarationThese variables can only be seen and used by their form's procedures.
PublicGlobalDuration of the formDeclared immediately after the form declarationThese variables can be seen and used by all forms or objects in the project.

Simple Variables

There are many different data types for variables. They are differentiated by declaring the variable as being of a particular data type. Each data type occupies a different amount of memory and represents a different amount of data. The rule of thumb is to use the data type which does the job, but uses the least amount of space in doing so. The following are the common data types which are frequently used in VB programming:

Complex Variables

Embedded Variables

An embedded variable is one which is defined inside another object. The most common example of this is the objects that make up the toolbox. Each of these has many properties which you can view and change in the PRoperties window. These properties are embedded variables. We will encounter these when we start working with classes and object-oriented programming.

Constants

When we need to use a value in our program, and we know that the value will not change during the execution of the program, we have two ways to do this. The first way was discussed above. We simply enter the data into our program where it is needed. This is the Literal we discussed above. There is another way, and for many uses, it is a better way of handling what we need to accomplish. It is called a constant. We declare a constant and store the value we need in it. When we need the value we simply use the constant name instead of the raw value. Constants can be declared anywhere you can declare a variable. By convention we list constants before variables when declaring them. There are actually two kinds of constants used in VB. They are as follows:

Constant Declarations

For user declared constants the syntax is similar to variable declarations with the addition of the keyword Const. The general form looks like this:

    ScopeIdentifier Const IDENTIFIER [as Datatype] = value

A brief explanation of what this means particularly how it is different form variable declarations.
  1. ScopeIdentifier is Dim for local, private or public for module, or public for global.
  2. Const is the key difference. The use of this word tells the program that this is a constant.
  3. IDENTIFIER, the name of the constant, is exactly like the variable declaration except that it is capitalized. This is a convention whose purpose is to make constants instantly identifiable as such.
  4. Datatype can be any legal data type as per variable declarations.
  5. Again the square brackets indicate the optional elements, but you should specify these for the same reasons outlined above.
  6. = value is the method of telling our program what the value of the constant is. Since constants may not be changed during the execution of the program, the value must be specified at the time of declaration and using this method. Actually the value of a variable may also be set by using this method. It is a good technique for setting a starting value for a variable which should not start at empty, zero or False.

Intrinsic Constants

There are a number of pieces of information which are repeatedly used by programmers. Many of these were anticipated by Microsoft developers and included as Intrinsic Constants. Two examples are colors and linefeeds. There are many predefined colors in Visual Basic. Some common examples are:

One of the more useful constants which is predefined in VB is the linefeed constant. Actually there are several of them but given the way Windows works the most useful is vbCrLf which is a nmemonic for Carrage Return Linefeed. This is the most common linefeed control and it causes anything following it to be displayed on the next line. It is treated just like a string and can be concatenated into any string. this is really useful for controlling text display in message boxes. A two line display can be created with this intrinsic contant.

     MessageBox.Show("Welcome" & vbCrLf & txtName.Text, "Greeting")

This will display two lines of text in a Message box as shown to the right.