Professional Documents
Culture Documents
2
Visual Basic Naming Rules
Use the following rules when you name procedures, constants, variables, and arguments in a Visual Basic
module:
• You must use a letter as the first character.
• You can’t use a space, period (.), exclamation mark ( ! ), or the characters @, &, $, # in the
name.
• Name can’t exceed 255 characters in length.
• You can’t repeat names within the same level of scope. For example, you can’t declare two
variables named age within the same procedure. However, you can declare a private variable
named age and a procedure-level variable named age within the same module.
Note: Visual Basic isn’t case-sensitive, but it preserves the capitalization in the statement where the name
is declared.
Adding Comments
Comments provide a convenient means to document a program. Visual Basic ignores comments when it
compiles your program. Comment lines begin with a single apostrophe ( ' ), followed by a textual message,
and can be added anywhere in a procedure. By default, comments are displayed as green text.
Example:
Assignment Statements
Numeric Constants
Numbers are referred to as numeric constants in Visual Basic. Most numeric constants are expressed as
integers (whole numbers that do not contain a decimal point or an exponent), long integers (similar to
integers with an extended range), single-precision real quantities (numbers that include a decimal point, an
exponent, or both), or double-precision real quantities (similar to single-precision real quantities with an
extended range and greater precision). The following rules apply to numeric constants:
0 1 +1 0.1E+1 10E-1
-5280 -5028E+3 1492 3000000 3D+6
String Constants
A string constant is a sequence of characters (i.e., letters, numbers and certain special characters, such as
+, -, /, *, =, $, etc.), enclosed in quotation marks. Blank spaces can be included within a string. A quotation
mark can also be placed within a string, but it must be written as two adjacent quotation marks.
String constants are used to represent nonnumeric information, such as names, addresses, etc. There is no
practical restriction on the maximum number of characters that can be included within a string constant.
Thus, the maximum length of a string constant can be considered infinite.
Data Types
Visual Basic supports all common data types, including Integer, Long (i.e., long integer), Single, Double, and
String. The language also supports other data types, such as Boolean, Byte, Currency, and Date data, as
well as Variant-type and user-defined data types.
Boolean - indicates the presence of logical data that can contain either of two values, True or False.
Example:
Dim myVariable As Boolean
myVariable = True
Currency - provides a special numeric format to store monetary values. Currency data types always include
four decimal digits. This data type is useful for calculations involving money and for fixed-point calculations
in which accuracy is particularly important.
Date - contains a specially formatted number that represents the date or time with the range from 1 January
100 to 31 December 9999 for dates and 0:00:00 to 23:59:59 for times.
Double - stores a double precision floating point number with a range from -1.79769313486232E308 to
-4.94065645841247E-324 for Negative values, and 1.79769313486232E308 to 4.94065645841247E-324
for Positive values.
Integer - a whole number that ranges from -32,768 to 32,767. Integer variables were probably the most
used type of variables.
Long - a signed integer stored in four bytes of memory ranging from -2,147,483,648 to 2,147,486,647.
Object – Visual Basic uses object variables to store and refer to objects. Here are a few examples:
Single - a single precision number that represents fractional numbers, numbers with decimal places, or
exponential numbers. Negative values are ranging from -3.402823E38 to -1.401298E-45, with Positive
values ranging from 1.401298E-45 to 3.402823E38.
User-Defined Type – a user-defined type is a compound structure that holds different variables of simpler
data types. Before you can use a User-Defined Type variable, you must first define its structure, using a
Type statement in the declaration section of a module. For example:
Once you have defined a Type structure, you can create variables of that type as you would do with any
Visual Basic data type. You can then access its individual items using the dot syntax:
Variant – variant is the default data type for Visual Basic. In other words, if you use a variable without
specifying its type, as in the following line of code:
Decimal – a floating-point data type with a higher precision than Double, but it has a smaller range value.
Decimal is a singular case among the data types supported by Visual Basic in that you can’t explicitly
declare a variable using As Decimal. Instead, you assign a value to a Variant variable using the CDec
conversion function, for example:
Type Conversion
Visual Basic also supports conversion from one data type to another. Some conversion functions supported
by Visual Basic are:
Function Description
CDbl(variable_name) Converts a variable to a Double precision data type. The function accepts
any number within the limits of the Double data type or any string data that
can be converted to a number within the range of the double data type.
CInt(variable_name) Converts a variable to an Integer data type. CInt accepts any number
within the limits of the integer data type or any string data that can be
converted to a number and is within the limits of the integer data type.
CLng(variable_name) Converts a variable to a Long data type. The function accepts any number
within the limits of the long integer data type or any string data that can be
converted to a number whose value lies within the range of a long integer.
CSng(variable_name) Converts a variable to a Single data type. The function accepts any number
within the limits of the single data type or any string data that can be
converted to a number within the range of the Single data type.
CStr(variable_name) Converts a variable to a String data type. CStr accepts any kind of data.
CVar(variable_name) Converts a variable to a Variant data type. CVar accepts any kind of data.
A variable is a name that represents a numerical quantity, a string, or some other basic data item (e.g., a
date, true/false condition, etc.). The following rules apply to the naming of variables:
1. A variable name must begin with a letter. Additional characters may be letters or digits. Certain
other characters may also be included, though the period and special data-typing characters (e.g.,
%, &, !, #, and $) are not permitted. In general, it is good programming practice to avoid the use of
characters other than letters and digits.
2. A variable name cannot exceed 255 characters. As a practical matter, however, variable names
rarely approach this size.
3. Visual Basic does not distinguish between uppercase and lowercase letters. Many programmers
use uppercase letters as word separators within a single variable name (e.g., FreezingPoint,
TaxRate, etc.).
4. Visual Basic includes a number of reserved words (e.g., Dim, If, Else, Select, Case, Do, etc.).
These reserved words represent commands, function names, etc. They cannot be used as variable
names.
Variable Declaration
In this syntax,
• Dim is the keyword that tells Visual Basic that you want to declare a variable.
• variable_name is the name of the variable.
• As is the keyword that tells Visual Basic that you’re defining the data type for the variable.
• DataType is the data type of the variable.
If you have a number of variables to declare, you can do this on a single line by separating them with
commas, as in the following Dim statement:
To declare a constant in Visual Basic, you have to use the Const statement and assign or set its value.
After a constant is declared, it cannot be modified or assigned a new value.
You can declare a constant within a procedure or at the top of a module, in the declarations section. Module-
level constants are private by default. To declare a public module-level constant, precede the Const
statement with the Public keyword. You can explicitly declare a private constant by preceding the Const
statement with the Private keyword to make it easier to read and interpret your code.
The following example declares the Public constant conAge as an Integer and assigns it the value
34.
You can declare one or more constants in a single statement. To specify a data type, you must include the
data type for each constant. In the following statement, the constants conAge and conWage are declared
as Integer and Currency, respectively.
In Visual Basic, you can implicitly declare a variable by simply using it in an assignment statement, as in the
following example:
Print displayThisSTRING
End Sub
All variables that are implicitly declared are of type Variant. Variables of type Variant require more memory
resources than most other variables. Your application will be more efficient if you declare variables explicitly
and with a specific data type. Explicitly declaring all variables reduces the incidence of name-conflict and
spelling mistakes.
Using the Option Explicit statement is good programming practice. It forces us to declare all
variables and constants. You can automatically have Visual Basic add this to new modules as they are
created by checking the Require Variable Declaration option, which can be found on the Editor tab of the
Options dialog (Select Tools >> Options). You can also used explicit declaration by placing the statement
Option Explicit in a module before any procedure.
When the Option Explicit statement is used, Visual Basic can determine variables you used in your
program that has not been declared.
Rather than declaring a data type explicitly (using Dim or Const statement), a variable or named constant
can be associated with a data type by adding a single-character suffix to the end of the variable/constant
name. several of the more commonly used suffixes are listed below.
Shown below are several variables whose data types are defined by suffixes.
The use of suffixes is derived from earlier versions of the Basic language, and is included in Visual Basic
largely for purposes of consistency and backward compatibility. Modern programming practice encourages
the use of explicit data type declarations rather than suffixes.
Sometimes you need a variable to be seen by all the procedures within your project, while other variables
should only be available within a particular procedure. The visibility of a variable is known as its scope.
Closely related to a variable’s scope is its lifetime, or the period of program execution when the variable is
live and available for use. Precisely where you declare a variable or constant in a program determines its
scope and its lifetime.
Global Variables
In Visual Basic jargon, global variables are those variables declared using the Public keyword in the
declaration section of Visual Basic modules. Conceptually, these variables are the simplest of the group
because they survive for the life of the application and their scope is the entire application.
Module-Level Variables
A variable has module-level scope when it can be accessed by all the subroutines and functions contained
in a particular module. Module-level variables are visible only from within the module they belong to and
can’t be accessed from the outside. To create a variable with module-level scope, you must declare it in the
module’s declaration section (that is to say, outside of any subroutine or function) by using either the Dim or
Private Statement.
Procedure-Level Scope
A variable that is declared within an individual procedure (that is, within a subroutine or a function) can only
be used within that procedure, and is therefore said to have procedure-level scope. The lifetime of a
procedure-level variable ends when the End Sub or End Function statement is executed.
Information and Communication Technology Department 20
Palompon Institute of Technology
VISUAL BASIC FUNDAMENTALS
2
There is also a special type of variable that has procedure-level scope, called a static variable. A static
variable is defined within a procedure, and although it has procedure-level scope, it has module-level
lifetime. In practice, this means that you can only use the variable within the procedure in which it’s defined,
but its value is maintained between calls to the procedure. To declare a static variable, you use the Static
keyword in a procedure, for example:
Declaring a variable within a procedure must be done using the Dim or Static statement; you can’t
declare a variable or constant as Public or Private within a procedure.
Special symbols, called arithmetic operators, are used to indicate arithmetic operations such as addition,
subtraction, multiplication, division, and exponentiation. These operators are used to connect numeric
constants and numeric variables, thus forming arithmetic expressions.
+ Addition x+y = 13
- Subtraction x-y = 5
* Multiplication x*y = 36
/ Division (float) z/y = 2.375
\ Division (integer) z\y = 2
^ Exponentiation x^y = 6561
- Negation -y = -4
Mod Modulus x Mod y = 1
Order of Precedence
The following example shows how operators are evaluated in Visual Basic using the order of precedence
table given above.
Example 1:
z = p * r Mod q + w / x – y
1 3 4 2 5
Example 2:
y = ( a * x + b ) Mod ( b / 5 ) * u - 2
1 2 5 3 4 6