Chapter 15
VARIABLES AND OPER ATORS IN
VISUAL BASIC
15.1 VARIABLE NAMING CONVENTIONS
1. Must begin with a character.
2. Maximum length of variable is 255 characters.
3. Should not contain spaces or special characters except the underscore (_).
15.2 VARIABLE DECLARATION
To declare the variable private use DIM
DIM < variable name > as < Data Type >
For example, DIM productname as String
DIM quantity as Integer
To declare the variable public use PUBLIC
PUBLIC < variable name > as < Data Type >
297
298 SOF T WARE ENGINEERING AND TESTING
For example, PUBLIC productname as String
PUBLIC quantity as Integer
15.3 SCOPE OF VARIABLES
Type of
Declaration
Section of
Declaration
Scope
DIM Procedure Can be accessed by that particular procedure
only where the variable has been declared.
DIM General declaration
section of the form
Can be accessed by all the procedures of the
form and the value can also be transferred
from one procedure to another.
DIM General declaration
section of module
Can be accessed by all the procedures and
the value can be used in that procedure only
where it has been assigned to the variable. The
value of the variable cannot be transferred to
another procedure.
PUBLIC General declaration
section of module
The variable and value both can be accessed
in any procedure of all the forms.
Duplicate declaration of the variable is not allowed in the current scope.
15.4 LOGICAL OPERATORS
Operator Function of Operator Example Return Value
+ Adds two numbers A=10+10 20
Subtracts right number from left
number
A=20–10 10
* Multiplies two numbers A=20*10 200
/ Divides left number by right
number
A=20/6 3.33
\ Divides left number by right number
and returns only integer number
A=20\6 3
VARIABLES AND OPERATORS IN VISUAL BASIC 299
MOD Divides two numbers and returns
remainder
A=20 mod 6 2
^ Used to express the power of an
exponent
A=10^10 100
& Used to concatenate two strings Myname=
“Mahesh” & “Gupta”
“Mahesh
Gupta”
15.5 LOGICAL OPERATORS
1. AND
Checks both the expressions and returns True if both the expressions are true. If
one of the expressions is False or Null, it returns False.
1
st
Expression 2
nd
Expression Result
T R U E T R U E T R U E
TRUE FALSE FALSE
F A L S E T R U E F A L S E
FALSE FALSE FA LS E
T R U E N U L L N U L L
FALSE NULL FALSE
NULL FALSE FALSE
NULL TRUE FALSE
NULL NULL NULL
2. EQV
Checks whether two expressions are identical or not. If both expressions are
identical, returns True, otherwise returns False. If any expression is Null, the return
value is also Null.
1
st
Expression 2
nd
Expression Result
T R U E T R U E T R U E
FALSE FALSE TRUE
TRUE FALSE FALSE
FALSE TRUE FALSE
300 SOF T WARE ENGINEERING AND TESTING
3. NOT
Used to reverse the expression.
Expression Result
TRUE FALSE
FALSE TRUE
NULL NULL
4. OR
If either of the two expressions is True, the result is True.
1
st
Expression 2
nd
Expression Result
T R U E T R U E T R U E
FALSE FALSE FALSE
N U L L N U L L N U L L
T RU E FA L SE TR UE
TRUE NULL TRUE
F A L S E T R U E T R U E
FALSE NULL NULL
N U L L T R U E N U L L
NULL FALSE NULL
5. XOR
If only one expression is True, the result is True.
1
st
Expression 2
nd
Expression Result
T R U E F A L S E T R U E
FALSE TRUE TRUE
T R U E T R U E FA L S E
FALSE FALSE FALSE
VARIABLES AND OPERATORS IN VISUAL BASIC 301
15.6 IF-ELSE STATEMENT
Case 1
If <Condition> Then
<Statement 1>
<Statement 2>
Else
<Statement 3>
End If
Checks the condition, if True, it executes Statement 1 and Statement 2;
otherwise, Statement 3 is executed.
Case 2
If <Condition 1> Then
<Statement 1>
ElseIf <Condition 2> then
<Statement 2>
End If
All conditions are evaluated sequentially. If True, the corresponding statement
is executed.
15.7 DO WHILE …. STATEMENT
Case 1
Do While <Condition>
<Statement>
Loop
Performs the Do While loop and the Statement is executed while the condition
is True.
Case 2
Do While Not <Condition>
<Statement>
Loop
Performs the loop until the condition becomes True.
You can use Exit Do to terminate the Do While Loop.
Do While <Condition>
<Statement>
Exit Do
Loop

Get Software Engineering and Testing now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.