- You use variables to temporarily store values during the execution of an application.
- Variables have a name (the word you use to refer to the value the variable contains) and a data type (which determines the kind of data the variable can store).
- You can think of a variable as a placeholder in memory for an unknown value.
Storing and Retrieving Data in Variables
You use assignment statements to perform calculations and assign the result to a variable:
ApplesSold = 10 ' The value 10 is passed to the
' variable.
ApplesSold = ApplesSold + 1 ' The variable is
' incremented.
Declaring Variables
To declare a variable is to tell the program about it in advance. You declare a variable with the Dim statement, supplying a name for the variable:
Dim variablename [As type]
Variables declared with the Dim statement within a procedure exist only as long as the procedure is executing. When the procedure finishes, the value of the variable disappears. In addition, the value of a variable in a procedure is local to that procedure — that is, you can't access a variable in one procedure from another procedure. These characteristics allow you to use the same variable names in different procedures without worrying about conflicts or accidental changes.
A variable name:
- Must begin with a letter.
- Can't contain an embedded period or embedded type-declaration character.
- Must not exceed 255 characters.
- Must be unique within the same scope, which is the range from which the variable can be referenced — a procedure, a form, and so on.
The optional As type clause in the Dim statement allows you to define the data type or object type of the variable you are declaring. Data types define the type of information the variable stores.
There are other ways to declare variables:
Ø Declaring a variable in the Declarations section of a form, standard, or class module, rather than within a procedure, makes the variable available to all the procedures in the module.
Ø Declaring a variable using the Public keyword makes it available throughout your application.
Ø Declaring a local variable using the Static keyword preserves its value even when a procedure ends.
Implicit Declaration
You don't have to declare a variable before using it. For example, you could write a function where you don't need to declare TempVal before using it:
Function SafeSqr(num)
TempVal = Abs(num)
SafeSqr = Sqr(TempVal)
End Function
Visual Basic automatically creates a variable with that name, which you can use as if you had explicitly declared it. While this is convenient, it can lead to subtle errors in your code if you misspell a variable name. For example, suppose that this was the function you wrote:
Function SafeSqr(num)
TempVal = Abs(num)
SafeSqr = Sqr(TemVal)
End Function
At first glance, this looks the same. But because the TempVal variable was misspelled on the next-to-last line, this function will always return zero. When Visual Basic encounters a new name, it can't determine whether you actually meant to implicitly declare a new variable or you just misspelled an existing variable name, so it creates a new variable with that name.
Explicit Declaration
To avoid the problem of misnaming variables, you can stipulate that Visual Basic always warns you whenever it encounters a name not declared explicitly as a variable.
To explicitly declare variables
Place this statement in the Declarations section of a class, form, or standard module:
Option Explicit
– or –
From the Tools menu, choose Options, click the Editor tab and check the Require Variable Declaration option. This automatically inserts the Option Explicit statement in any new modules, but not in modules already created; therefore, you must manually add Option Explicit to any existing modules within a project.
The Option Explicit statement operates on a per-module basis; it must be placed in the Declarations section of every form, standard, and class module for which you want Visual Basic to enforce explicit variable declarations. If you select Require Variable Declaration, Visual Basic inserts Option Explicit in all subsequent form, standard, and class modules, but does not add it to existing code. You must manually add Option Explicit to any existing modules within a project.
No comments:
Post a Comment