Variables:
Variables can temporarily store the value of data in memory
during the execution of your program. They are often referred to as containers
for various types of data. But in reality, they are address placeholders to a
memory location in your computer.
Constants: Constants
directly contrast variables by retaining their values throughout your program’s
execution, whereas variables can lose or change their values during program
execution. Once you declare a constant, its value cannot change during program
execution.
When declaring
Identifiers, you should be aware of a few naming conventions. An Identifier’s
name
◆Must begin with a letter, followed by
more letters or digits.
◆Can’t contain embedded periods or other
special punctuation symbols. The only special character that can appear in a
variable’s name is the underscore character.
◆Mustn’t exceed 255 characters.
◆Must be unique within its scope. This
means that you can’t have two identically named variables in the same
subroutine, but you can have a variable named counter in many different
subroutines.
◆Variable names in VB are case-insensitive
Scope: The scope (or
visibility) of a variable is the section of the application that can see
and manipulate the variable. If a variable is declared within a procedure, only
the code in the specific procedure has access to that variable; this variable
doesn’t exist for the rest of the application. When the variable’s scope is
limited to a procedure, it’s called local.
Sometimes,
however, you’ll need to use a variable with a broader scope; a variable that’s
available to all procedures within the same file. This variable, which must be
declared outside any procedure, is said to have a module-level scope.
Another type of scope is the block-level scope. Variables
introduced in a block of code, such as an If statement or a loop, are local to
the block but invisible outside the block.
Lifetime: lifetime is the period
for which they retain their value. Variables declared as Public exist for the
lifetime of the application. Local variables, declared within procedures with
the Dim or Private statement, live as long as the procedure. When the procedure
finishes, the local variables cease to exist, and the allocated memory is returned
to the system. Of course, the same procedure can be called again. In this case,
the local variables are re-created and initialized again. If a procedure calls
another, its local variables retain their values while the called procedure is
running. You also can force a local variable to preserve its value between
procedure calls by using the Static keyword.
Variables
declared in a module outside any procedure take effect when the form is loaded
and cease to exist when the form is unloaded. If the form is loaded again, its
variables are initialized as if it’s being loaded for the first time.
Variables are
initialized when they’re declared, according to their type. Numeric variables are
initialized to zero, string variables are initialized to a blank string, and
object variables are initialized to Nothing