Chapter -4
DATA MANIPULATION
Concept of Variables & constants
VARIABLES
• A variable is like a storage box for various types of
information.
• I is used for temporarily storing the data.
• Using variables makes it easier to reference information in a
program.
• There are different types of variables.
Its value can be varied , or changed.
• In this example ,initially the value stored
in the Variable myNum was 4.
• Later it became 5.
• Variable is a location in computer’s memory where
a data (value) can be stored, which can be used by
the program in later stages if required.
• For eg: Instead of typing “Visual Basic Programming”
over and over again you can make a variable called X
to store the word.
• When you want to use it, all you have to do is type ‘X’.
X = “Visual Basic Programming”
RULES FOR NAMING A VARIABLE
• It must be less than 255 characters.
• No spacing is allowed.
• It must not begin with a number.
• Period (.) is not permitted.
VALID NAME INVALID NAME
My_Car [Link]
This_year This year
Long_Name_Can_be_Used He&His Father ( *& is not acceptable)
Var_2 2Var
CONSTANT
A constant is a value that does not change
during the execution of a procedure.
The constants are declared as follows:
Const Pi as Single =3.14159
In no case can a constant be altered during the execution of
the project.
Constants can be declared anywhere in the project and follow
the same scope rules as variables.
Types of Visual Basic Data
DATA TYPE
NUMERIC NON –NUMERIC DATA
DATA TYPE
NUMERIC NON –NUMERIC DATA
INTEGER CURRENCY SINGLE STRING DATE BOOLEAN
●
It is used to represent numbers.
●
The default value of an integer variable is 0.
INTEGER ●
You can not use integer data type to store decimal values
●
If you assign a decimal value to an integer type variable the
data value is rounded of to the nearest integer.
●
This data type is used for fixed point calculation
CURRENCY ●
in which accuracy is very important.
The default value of this variable is 0.
●
It is stored as a single precision floating point number.
SINGLE
●
If the number of decimal places exceeds the range of a single
precision floating point number, the last decimal place is
rounded.
●
The default value is 0.
●
To store data consisting of alphanumeric characters.
●
A string variable is initialized to empty.
STRING ●
An empty value implies that the variable is not initialized.
●
A string of variable length means that the size of the variable
depends on the value that is assigned to it.
Date data type is used to
DATE
●
represent date and time values.
●
To represent data that can have only
BOOLEAN ●
two possible values.
The default value is FALSE
Declaring and assigning value to a variable
In Visual Basic , one needs to declare the variables before using
them by assigning names and data types.
The format is as follows:
Syntax
Dim <variable name > As <Datatype>
They normally declared in the general section of the
code window using the Dim statement.
GENERAL SECTION
PICTURE
You may also combine them in one line, separating each
variable with a comma, as follows:
Dim x As string, tot As Integer, dob as Date
For string declaration , there are two possible format ,
one for the variable –length string and another for the
fixed-length string.
For the variable-length string just use the same format
as in the above example.
However , for the fixed –length string, you have to use
the format as shown below:
Dim variableName as String * N
where N defines the number of characters the string
can hold.
Example:
Dim StuName as String * 10
StuName can hold not more than 10 characters.
Assigning Values to Variables.
After declaring various variables using the Dim statements,
we can assign values to those variables.
General format of an assignment is:
Variable = Expression or value
The variable can be a declared variable or a control
property value. The expression could be a mathematical
expression , a number , a string, a Boolean value ( true
or False) and, etc.
Examples:
Firstvalue= 100
Username=“Anjali”
X=2.5
[Link]=false
Y= X*2
Activity 1:
Program to display the value of variables on the click of a
command button in a textbox.
Code:
Output
Example:
Program to find the product of two numbers on the click of
a command button using variables.
Decimal values entered to integer variable:
Then the number 3.6 is ronded to 4 and 5.6 is rounded to 6.
Private and Public Variable
When the Dim statement is used , variables are only visible
in their current form/module. In order to allow a variable to
function throughout the project , you have to change the
variable’s “scope”. In other words, replace Dim with another
statement:
Dim or Private
If a variable is declared within a procedure, then it becomes a
local variable and is only visible to anything that is inside
that procedure.
Example:
Private MyNum as Integer
Public
A variable declared at the module or form level that is visible to
all procedures within the project.
Public variables must be declared in the
GENERAL DECLARATIONS SECTION.
Example: Public MyNum as Integer
makes MyNum visible to all forms/modules within the project.
Variables declared in general
section are called public variable
Private Variable
THANK YOU