TechnologyUK - Programming (VB6) Logo

Declaring Variables

A program can have as many variables as are needed. Before you can use a variable, however, you must declare the variable. To do this, you must tell Visual Basic the name and datatype of the variable. Once a variable is declared, you cannot change the datatype of the variable. If you store an integer in a single-precision variable, Visual Basic converts the integer to a single-precision value before storing the number in the variable. Such datatype conversions are common, and do not normally cause a problem. To declare a variable, you use the Dim statement, which assigns a name and a datatype to the variable. The format of the Dim statement is shown below.

Dim VarName As DataType

Varname is a name you supply, and DataType is one of the Visual Basic datatypes you have learned about. The location of the Dim statement within your application will determine how it may be used. If you include the Option Explicit statement in the General section of a form module or a standard module, you must declare all variables before you use them. Otherwise, you can start using a variable name without first declaring it, although Visual Basic assumes that the datatype is Variant. It is good practice to use the Option Explicit statement to avoid variable naming errors. If a Dim statement appears within in an event procedure, the variable is visible only within the event procedure, and is known as a local variable. If the Dim statement appears in the General section of a module, all procedures in that module can access the variable, which is said to be a global variable. If you replace Dim with Public in a General section, the variable is available to every module in the project. Some programmers use variable names that indicate the datatype of the variable (for example: int Integer intCount), although this is not a requirement.

When dealing with strings, strings remember that this type of variable can be either fixed or variable-length (up to 65,500 characters). A variable-length string variable can vary in size throughout the execution of the program, as different values are assigned to it. To limit the amount of text that a string holds, use the format shown below when declaring a string variable.

Dim strFirstName As String * 15

strFirstName is the name of a String variable that can hold up to fifteen characters. If the program tries to store a string with more than fifteen characters, the string is truncated, and only the first fifteen characters are stored.

Note that a newly declared variable does not have a value until one is assigned to it using an appropriate assignment statement. An assignment statement stores data in a variable (or a control, or some other object). The general format of the assignment statement is shown below.

VarName = Expression

VarName is the variable name defined using the Dim statement, and Expression can be a literal, another variable, or a mathematical expression. The datatype of Expression must match the datatype of the variable to which you are assigning it (you cannot, for example, assign a string value to a single-precision variable), although Visual Basic can carry out a quick conversion in many cases. A literal value in Visual Basic is always of the datatype Variant, so in the statement shown below, Visual Basic converts the literal (Variant) value to a Single before storing it in the variable sngPrice.

sngPrice = 24.99