The Values of an Application


Introduction to Variables



When interacting with the computer, at times, a user might be asked to provide a value. At other times, the user might be presented with a value to manipulate. During this interaction, the values, any types of values, are stored and retrieved at will. To make this possible, the computer uses its memory. The computer memory is an area used to receive information, store it, and then make that information available when requested. The computer only makes its memory available to you. It doesn't know and cannot predict the type of value that your particular program would need. This is because one program might be made to manipulate numbers while another program might be made for names. Because the information stored in the computer can be as varied as possible, it may be segmented (the word segment is taken in its English meaning and not in its Assembly definition) in portions or various pieces.

The pieces of information stored in the computer memory come, go, and change on a regular basis. For this reason, a piece of information stored in the computer memory is called a variable. A variable is an area of the computer memory used to store a value. Because there can be many kinds of values stored in the computer memory, a variable stores only one kind of value. 

The Stack

To manage the various types of values that can be used in a program, the computer is equipped with two types of memory. The Random Access Memory, also called RAM, is a type of memory that is "filled" with values when the computer comes up and while a person is using the computer. When the computer gets shut down, the RAM looses everything that was put in that memory so that the next time the computer comes up, the RAM "forgets" the values that were entered in it the previous time. In other words, when the computer boots up, the RAM is empty and new values must be entered in it.

Another type of memory that the computer uses is called Read-Only Memory (ROM). In this area, the computer stores values that "stick" in it and stay there. Such values can be called when needed and, unless they are explicitly deleted, when the computer gets shut down, the values go back to the ROM. The next time the computer comes up, the values of the ROM will be available. The values of the ROM stay there and are retrieved only when requested.

The RAM can be illustrated like a group of small boxes. When the computer boots up, or while the computer is coming up, they are empty:

When the computer has finished booting up, some values are asked to occupy this value. In the same way, when you start a program like Notepad, it is "loaded" in this memory. This means that part of the RAM is filled with some values. Some other parts of the RAM are filled with garbage or values that don't mean anything to you (this is illustrated with numeric values in the following table):

When your application comes up, you can ask the computer to put some values for your application. Remember that you are not, or your application is not, the only one that needs to use an area of the computer memory. Because there can be so many values that different applications store, retrieve, and manipulate all day long or as long as the computer is on, the operating system reserves an area of this memory for applications like yours. To make sure there is no mess, the computer is in charge of this area of memory. When you want to store a value in that memory, you let the computer know. The computer then puts (the verb used is "push") that value in the necessary area of the memory (the computer, and not you, decides where to put the value). When the value is not used anymore, the computer removes (the verb used is "pop") that value. This allows the computer to make that area of memory available to the applications that would need it. This area of memory that the computer reserves for the various applications, and is in charge of, is called the stack.

Variable Declaration

If you want to use an area of memory of the computer, in other words if you want to store a value in that memory, you must ask the compiler to reserve a portion of memory for your value. Making this request is referred to as declaring a variable. To formulate this request, you must first type the Dim keyword:


To make this request, you must provide a name for the variable. The name of a variable is also called an identifier. When the computer reserves an area of memory, it uses a name to be able to locate that area of memory. Later on, when you want to store a value in that memory, you will communicate a name to the compiler. The compiler will know the area of memory you are referring to by its name. This means that, at the right time, the name is used by both you and the compiler to know what portion of memory you are referring to. The name itself is not stored in the memory per se. It only allows you and the compiler to access a particular area of memory.

The Name of a Variable

There are rules you should, and usually must, follow when naming your variables. The rules to follow are:

  • The name of a variable can start with with a letter (a, b, c, d, e, f, g, h, i, f, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z. A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, or Z) or an underscore _

  • After the first character that is a letter or an underscore, the name can contain letters, underscores, or digits (0, 1, 2, 3, 4, 5, 6, 7, 8, or 9)

  • If the first character is an underscore, it must be followed by a letter

  • The name cannot have an empty space

  • The name cannot have any special character (! @ # $ % ^ & * + - = ~ ` < > , . : ; " ' { [ } ] | \ ) between letters or digits except the underscore as specified above. Some characters can be used as the last character of a name. We will encounter them when necessary

  • Can have up to 255 characters but refrain from using a name that is over 32 characters

  • Must be unique inside of the procedure or the module it is used in

A name cannot be one of the words reserved for VBasic's own use. These words are also called keywords. Therefore, avoid using the following words to name a variable

AddHandler AddressOf Alias And AndAlso
Ansi As Assembly Auto Boolean
ByRef Byte ByVal Call Case
Catch CBool CByte CChar CDate
CDbl CDec Char CInt Class
CLng CObj Const CShort CSng
CStr CType Date Decimal Declare
Default Delegate Dim DirectCast Do
Double Each Else ElseIf End
EndIf Enum Erase Error Event
Exit False Finally For Friend
Function Get GetType GoSub GoTo
Handles If Implements Imports In
Inherits Integer Interface Is Let
Lib Like Long Loop Me
Mod Module MustInherit MustOverride MyBase
MyClass Namespace New Next Not
Nothing NotInheritable NotOverridable Object On
Option Optional Or OrElse Overloads
Overridable Overrides ParamArray Preserve Private
Property Protected Public RaiseEvent ReadOnly
ReDim REM RemoveHandler Resume Return
Select Set Shadows Shared Short
Single Static Step Stop String
Structure Sub SyncLock Then Throw
To True Try TypeOf Unicode
Until Variant Wend When While
With WithEvents WriteOnly Xor  

Although you must always avoid using keywords as names of your variables in your program, if you insist on using one of these keywords to name something, put the word between square brackets. An example would be [Step]

VBasic is not case sensitive. This means that NAME, name, and Name represent the same word. This means that, in the same section (normally called scope), you cannot have two variables with the same name that differ only by their cases. This would cause a name conflict. If you declare a variable in a scope and use it later with a different case, as long as the same characters are used on both names, the VBasic compiler would know what variable you are referring to and there would not be any conflict.

Option Explicit

You should always make sure that you declare a variable before using it. Otherwise you may use two variables that seem to be different but because of a mistype, you would think that you are using two variables. Examples are Type and Tape. To indicate to the compiler that each variable must be declared prior to being used, in the top section of your source file, you should type:

Option Explicit On



Previous Copyright © 2004-2007 FunctionX, Inc. Next