VARIABLES IN VISUAL BASIC New
VARIABLES IN VISUAL BASIC New
Dim [Variable Name] As [Data Type] = [Value]
Dim It is useful to declare and allocate the storage space for one or more variables.
[Variable Name] It’s the variable's name to hold the values in our application.
As The As clause in the declaration statement allows you to define the data type.
[Data Type] t’s a type of data the variable can hold, such as integer, string,
decimal, etc.
Dim Foo
is equivalent to
Syntax:
Dim variable As [Type]
Example:
Private Sub cmdSum_Click()
Dim m As Integer
Dim n As Integer
Dim sum As Integer
sum = m + n
End Sub
This forces the user to declare all the variables. The Option Explicit
statement checks in the module for usage of any undeclared
variables and reports an error to the user. The user can thus rectify
the error on seeing this error message.
The Option Explicit statement can be explicitly placed in the general
declaration section of each module using the following steps.
Click Options item in the Tools menu
Click the Editor tab in the Options dialog box
Check Require Variable Declaration option and then click the OK
button
Categories of Variables
Static variables
They are those variables that don’t change when the program
executes.
Dynamic variables
They are those variables that change when the program executes.
Scope of variables
Once a procedure is executed, the values of its local variables are lost
and the memory used by these variables is freed and can be
reclaimed. Variables that are declared with keyword Dim exist only as
long as the procedure is being executed.
Static Variables
Static variables are not reinitialized each time Visual basic Invokes a
procedure and therefore retains or preserves value even when a
procedure ends.
These static variables are also ideal for making controls alternately
visible or invisible. A static variable is declared as given below.
Static intPermanent As Integer
example of an event procedure for a
Command Button that counts and
displays the number of clicks made.
Each data type has limits to the kind of information and the
minimum and maximum values it can hold.
3. Date
Used to store date and time values. A variable declared as date type can
store both date and time values and it can store date values 01/01/0100 up
to 12/31/9999. used to contain date values, time values, or date and
time values
Datatypes cont’
4. Boolean
Boolean data types hold either a true or false value.
These are not stored as numeric values and cannot
be used as such. Values are internally stored as -1
(True) and 0 (False) and any non-zero value is
considered as true.
5. Variant
Stores any type of data and is the default Visual
Basic data type. In Visual Basic if we declare a
variable without any data type by default the data
type is assigned as default.
Operators in Visual Basic
Arithmetic operators
Operator Description Example (a = 6, b = 3)
Print "True"
Else
Print "False"
End If
Logical operators
LOGICAL OPERATORS
Example (a = True, b =
Operator Description
False)
It will return true if both
And a And b = False
operands are non zero.
It will return true if any one
Or operand becomes a non a Or b = True
zero.
End Sub
AND
End Sub
Not
a = True
b = False
y = Not a
Print y
End sub
XOR
Private Sub Command1_Click()
Dim a As Boolean, b As Boolean, y As Boolean
a = True
b = False
y = a Xor b
Print y
End Sub
concatenation operators
The following table lists the different types of concatenation operators available in Visual Basic.
Example (a = Hello, b =
Operator Description
World)
By using comment symbol ('), we can comment on the code in our Visual
Basic programming. The comment symbol (') will tell the Visual Basic
compiler to ignore the text following it, or the comment.