Programmer's Perspective ] Ins and Outs ] WhereCode ] [ Typology ] Alphabetic Information ] Arrays and Looping ] Builtins ] Randomization and Simulation ] Controls ] Creating and Using Files ] Errors ] Key Codes ] Drive Office With VB ]

 

 


The Typology of Variables

Classifying into types is something psychologists love to do.  Well, VB does also.

VB's variable types refer to what kind of data are stored under the variable's name and how the data should be stored in memory (which affects speed of operations). 

Types should be assigned to variable names by the programmer explicitly with any of statements: DIM, PRIVATE, PUBLIC (e.g. PRIVATE STDEV AS DOUBLE).  Which typing statement is needed depends upon the scope of the variable.   If its value is needed only within a subroutine, such as when a click event is fired, then use DIM.  If the value is needed throughout the subroutines in a Form, so that as long as the user can activate any of the Form's subroutines, the value is needed, then use PRIVATE and place the statement within the Declarations section.  If the current value of the variable is needed throughout the program, use PUBLIC and declare it in a .BAS Module.

The alphabetic, or string, variable, is used for names or for communication with the user.  The characters are reproduced literally when the variable is invoked.  Alphabetic variables may be entered via keyboard input or by replacement statements employing quotation marks, e.g., FIRSTNAME = "David".  They may be combined in an arithmetic-like fashion.  For example, if LASTNAME = "Weiss", then WHOLENAME = FIRSTNAME + LASTNAME would yield a new variable, WHOLENAME, whose value would be "DavidWeiss".  To make this variable look better for printing, I could instead have defined WHOLENAME = FIRSTNAME + " " + LASTNAME.  Alternatively, I could have put the space directly in the variable as LASTNAME = " Weiss".  A possible declaration statement would be:

PUBLIC LASTNAME AS STRING

Ordinary arithmetic variables are known as single precision.  They are accurate to seven digits, although you can make them be printed with as many decimal places as you please.  Single precision is VB's default for numerical variables.  Single precision variables may be used for any kind of arithmetic operations, although you may prefer to use other types.  An example is:

DIM GRANDMEAN AS SINGLE

Double precision variables provide fifteen significant digits and are therefore preferable to the default single precision in applications calling for high degrees of numerical accuracy.  In complex statistical calculations, I use double precision routinely because imprecision cumulates.  Double precision is also needed if very large numbers might arise.  If an "Overflow" error message occurs, it is usually because a quantity has exceeded the numerical limit for the specified type.  The down side of using double precision variables is that they occupy twice as much memory and slow program execution.  Single and double precision variables are referred to as real numbers (or floating-point numbers) in programming manuals; they are thereby distinguished from integers, which carry no decimal information.  An example is:

PRIVATE GROSSNATIONALPRODUCT AS DOUBLE

The use of integer variables whenever possible is highly recommended because computations involving them execute more quickly than those employing real numbers, sometimes dramatically so in large programs.  They also occupy less storage space.  When can you use integers?  Obviously if no decimal places are needed.  The most important cases are for dimensioning and for indices in loops; also you should always use integers for keeping track of indicators (as in one if by land, two if by sea...).  Integer values must be between -32,768 and +32,768.  If you anticipate that an integer variable might need to take on a larger value, the long integer is available.  Allowed values are between -2147483647 and +2147483647.  Long integers are ideal for large numbers when no decimal information is needed.  It is a long-standing tradition to begin integer variables with a letter from I through N inclusive, although VB is totally permissive about variable names.  Examples include:

DIM I AS INTEGER

PRIVATE NSUBJECTS AS INTEGER  

PUBLIC I1 AS LONG

Integer division is an arithmetic operation unfamiliar to most of us.  Symbolized by a backslash (\) as opposed to the forward slash (/) denoting ordinary division, integer division rounds both numerator and denominator prior to the division, and also truncates the quotient to an integer.  Thus 3.6\1.2 yields 4, and 10\4 yields 2.  Beware that VB will not catch your mistyping of a division sign, as both kinds are legitimate and the compiler cannot judge what you intended.  Truncation will also occur if an integer variable carries the result of an ordinary division; if J has been typed as an integer, then J = 10/4 yields a value of 2 for J.

In ancient times, programmers used to keep track of a logical variable (whose possible values are True and False) with an integer whose value was either 1 or 0.   You can still do that, but it is more efficient to use the Boolean (George Boole was a celebrated logician) type.  The Boolean variable occupies less memory.  When a variable is declared as Boolean, its possible values are True and False.  The initial state imposed by VB is False.  An example is:

DIM PAIDBILL AS BOOLEAN

What if you forget/don't bother to explicitly declare a variable?

VB will use its default type, which is called VARIANT.  This type is the slowest and occupies the most memory.  There are few situations (I can't think of any, but there must be some or why would it exist?) where a variant is the optimal type to use.   For a small program, the inefficiency is unlikely to be costly,  but failing to declare is a bad habit because eventually you will create a program that is resource-intensive.  The routine employment of OPTION EXPLICIT will prevent this bad habit.