9/28/2024
Variables
Variables §An allocated space in the memory
(RAM) for storing data.
ICT 202 §It is a placeholder, a container for a
specific vale.
Syntax Naming Rules of variables
Dim VarName As DataType • A variable name must begin with an alphabet.
in this syntax, • It cannot be more than 255 characters.
Dim i as Integer
• The variable name must not contain any special
character like %,&,!,#,@ or $.
Dim is the keyword that tells Visual Basic that you want to
declare a variable. • And finally, it has to be unique within the same
VarName is the name of the variable. scope.
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.
Visual Basic 6 the following variable Integer
Type Suffix
%
Declaration
Dim x as integer or Dim x %
types are available: Single ! Dim x as single or Dim x !
Double # Dim x as double or Dim x #
1. Boolean (Dim Foo as Boolean) String $ Dim x as string or Dim x $
Boolean is the simplest variable type as it can contain only
Boolean None Dim x as Boolean
one of two values: True or False.
2. Integer (Dim Counter As Integer) Date None Dim x as date
a variable will only contain whole numbers (such as 9) and
not fractional numbers Currency @ Dim x as Currency or Dim x
@
3. String (Dim Fruit as String) Long & Dim x as long or Dim x &
Fruit = "Banana" (Integer)
A string variable can contain an empty text, a character, a
word or a text of variable length. The string value must be
contained in quotation marks (").
1
9/28/2024
Two Types of Variables in VB 6.0 Form:
1. Implicit Variables: variables can be used without prior Example 5: write a program to add two integer
declarations but are suffixed with the following numbers after putting a suitable design.
symbols.
Example: Dim Numval%
2. Explicit Variables: variables can be used with prior
declarations.
Example: Dim Numval as integer
Code:
Given: constant value of x,y.
Let x=5 & y=3
Private Sub Command1_click
Dim x, y as integer
X=val(text1.text)
Y=val(text2.text)
Text3.text= X+Y
End Sub