|
Variables
- A variable is a location in the computers memory where a value can be stored for
use by a program.
link
- A variable name is any valid identifier.
- Variable names cannot be keywords and must begin with a letter and contain only letters,
numbers, and underscores.
- An identifier cannot begin with a
digit and cannot contain spaces.
- VB is NOT case-sensitive.
- Begin each identifier with lowercase letters in order to distinguish them from keywords.
- Choose meaningful variable names so that your programs are self-documenting.
- - - - - - - - - - -
- Keyword
Dim explicitly declares variables.
- The clause beginning with
As describes the variables type.
- Dim sum As Integer
declares a variable named
sum.
- Variables of the same type can be declared in separate
statements or in one statement with variables separated by commas.
-
A statement like
Dim
loanAmount, interestRate As
Integer declares both
loanAmount and
interestRate
as
Integers.
- - - - - - - - - - -
- Variables dimensioned outside of a
Sub are called form-level variables and are said to reside in the
general declarations, which makes them available to every event
procedure.
- Form-level variables come into
existence when the program is started and destroyed when the program
terminates.
- Form-level variables can be used
to store information between controls or between multiple clicks of the
same button.
- Form-level variables can cause
hard-to-trace program errors.
- Subroutine-level variables are
variables declared in a sub and can be referenced only in that sub.
- Subroutine-level variables are
created when they are declared in a sub, and destroyed when execution of
that sub terminates.
Initialize
all Variables
VB provides default values
for all variables. For example, integer variables default to 0.
It is better to initialize
your variables, because
-
some programming
languages do not provide default values and uninitialized variables may have
random values
-
others reading your
code do not know whether you wanted to accept the VB defaults or simply
forgot to initialize your variables
Option
Explicit
Option Explicit is imposed
by default. You must declare all variables before using them.
|
|
Constants
Constants are named memory
locations whose values that cannot change during
program execution.
Constants are often used to make a program more readable. The program
below demonstrates using constants.
Constants are declared using the keyword Const
or ReadOnly.
If Const
is used the constant
must be initialized with a constant expression when they are declared and cannot be
modified thereafter. If ReadOnly
is used the constant
must be initialized with a constant expression when they are declared (or in a
class constructor) and cannot be
modified thereafter.

|
|
Visual
Basic Data Types
Data types describe the information that a variable
can store, and also
describe how many bytes of memory are required to represent a type.

A Short
can store a value in the range -32,768 to 32,767.
An Integer
can store a value in the range -2147483648 to 2147483647.
A Long, which is 4 bytes, can store a whole number
in the range -9,223,372,036,854,775,808 through
9,223,372,036,854,775,807.
Boolean represents
True/False values.
The Byte
data type stores one byte of information.
Date
stores date and time formats.
Double
represents double-precision floating-point
numbers.
Single represents single-precision floating-point
numbers.
String
stores a series of characters.
Object stores any type of data and is the
default type.
Default values:
|
Data
Type |
Default
Value |
| Integer |
0 |
| Long |
0 |
| Single |
0.0 |
| Double |
0.0 |
| String |
"
" (blank) |
| Boolean |
False
(0) |
| Date |
0 |
|