VB UNIT - III Part - I
VB UNIT - III Part - I
VISUAL PROGRAMMING
The variable is a place holder or is a name given to memory location in the computer memory.by the
variable name , the value can be stored and retrieved from memory location.
The variable can have different names and different types of values. The different kinds of values that
one handled by variable are determined by the declaration of the variable
Example :
All these statements are called as declaration statements. The dim is keyword in visual Basic and has
language specific meaning. The compiler sets apart a number of bytes for each of the variable depending
on the data type of holds.
2. String
3. Date
4. Boolean
5. Variant
1. Numeric :
example:
dim a as Byte
example:
iii) Long : This Data Type is used to create variable that are store whole Numbers.It reserve 4 Byte of
Memory. This Store integer values in the range of (-2,147,483,468) to (+ 2,147,483,468)
example:
dim a as Long
iv) Single : This Data Type is used to create variable that are store fractional Numbers .It reserve 4 Byte of
Memory. This Store floating point value in the range of (-3•4 X 10-38) to (+ 3.4 x1038) appx (6 digits)
example:
dim a as Single
v). Double : This Data Type is used to create variable that are store fractional Numbers .It reserve 8 Byte
of Memory. This Store large floating value in Ranges from -1.79*10308 to 1..79*10308 appx (14 digits)
example:
dim a as double
vi) Currency: Store monetary values. It supports 4 digits to the right of decimal point and 15 digits to the
left
example:
dim a as currency
2. String :
This Data Type is used to create variable that are store string data or character or textual data .It
reserve 10+ Byte of Memory.A variable length string can store approximately 4 billion characters
example:
dim a as string
This Data Type is used to store date and time values. It reserve 8 Byte of Memory.
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
example:
dim a as date
4. Boolean
This Data Type is used to create variable that are used in testing condition. It reserve 2 Byte of
Memory. Boolean data types hold either a true or false value. 0(zero) => it represent FLASE, Non-Zero
value represent TRUE
example:
dim a as boolean
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. This Data Type is used to create variable that are used to store all kind of data. By
default the data type is assigned as default.
example:
dim a as variant
The scope of variable determines where you can access that variable in your code. If a variable is in
scope you can read or set its value. If it is out of scope you will not be able to access it.
There are three types of scope for variables in visual basic:
Global scope :
Global variables are in scope anywhere in your application
Module scope :
Module level variables are in scope anywhere within the module where they are declared
Local scope :
Local variables are only in scope within the procedure where they are declared
Global scope
Global variables are available anywhere in your program. Any line of code in any procedure can
read or write the value of the variable. While convenient, it is considered bad programming practice to
over use global variables and some programmers (myself included) male a considerable effort to avoid
them entirely
To create a global variable, declare it in the declarations section of a standard module using the
global or public keyword.
Module scope
Module level variables are available to any code within the module where they are declared.
While using global variables is considered bad programming practice, using module level
Variables is not module level variables allow you to share data between procedure in the application
To create a module level variable, declare it in the declarations sction of a module using either the dim or
private keyword
Local scope
Local variables are only available to the procedure in which they are created. Local variables are
the most restricted in scope not even other procedure in the same module may read or modify local
variables
You create local variables by declaring them with the dim or static keyword within the body of a
sub, function, or property procedure
1. Arithmetic operators
The operators are used for the basic arithmetic operations are called arithmetic operators.
VB provides the operators for addition,Substraction,multiplication,integer,division,floating point
division, modus and power(exponentiation) operations
2. Relational operators
VB allow us to use another set of operators called as logical operators. The operands to
logical operators have to be logical values true or false (1 or 0) the result of logical operation is
also true or false (1 or 0)
4. Bitwise operator
The bitwise operators allow the programmers to test and set individual bits or bit in a byte
data integer data and string type data
syntax :
If condition Then
Statement
End If
example :
End If
Flow chart
If condition Then
Statement-block1
Else
Statement-block2
End If
example :
Else
End If
Flow chart
If condition1 Then
Statement-block1
Statement-block2
Statement-block3
Else
Statement-block4
End If
The conditions are evaluated from the top and when one of them is true, the
corresponding block of statements is executed and control transfers to the statement following the Endif
& skips all other Elself clauses along with Else clause statements block. When none conditions are
true, the Else clause will be execute control transfers to the statement following the Endlf.
example :
When If..Then control structure is used inside another If... Then the resulted structure looks like
nested and is called as nested If control structure. This would appear as
Syntax:
If condition1 Then
Statement-block1
Else
If condition2 Then
Statement-block2
Else
If condition3 Then
Statement-block3
Else
Statement-block4
End If
EndIf
EndIf
Or
The Select... Case structure takes single expression or a value at the top of the structure and which
will be compared with several case values inside the structure. If expression value and case value match,
the statements in that case are executed.
case value_1:
statement1;
case value_2:
statement2;
case value_n:
statement n;
Case Else :
statement x;
End Select
End select
Flow chart
The syntax is :
Where the expression is any relational or comparative expression, the TruePart is the values which is
returned when the expression results true and the False Part is the value, which returned when the expression
results true.
1. While... Wend
2. Do...Loops
Do...while...Loop
Do...Until...Loop
Do...Loop...While
Do...loop...Until
3. For... Next
While Condition
statement-block
Wend
Dim i As Integer
While i <= 10
i=i+1
Wend
End Sub
syntax :
Do While Condition
Statement-block
Loop
Dim i As Integer
Do While i <= 10
i=i+1
Loop
End Sub
Do... Until...loop
In Do... Until...loop the loop is executed until the condition becomes true, the key word
until specify the condition how long the loop statements will be executed.
Syntax:
Do Until Condition
Statement-block
Loop
Dim i As Integer
Do Until i > 10
i=i+1
Loop
End Sub
Do...Loop... While
In the Do...Loop ... While, once the loop is executed and the condition is evaluated at the
end of the loop. The loop will be repeated while the condition is true, the key words while specify the
condition how long the loop statements will be executed.
Syntax:
DO
Statement-block
Dim i As Integer
Do
i=i+1
End Sub
Do...loop...Until
In do loop ... Until, once the loop is executed and the condition is evaluated at the end of the loop. The loop
will be repeated until the condition becomes true, the key word until specify the condition how long the loop
statements will be executed.
syntax :
Do
Statement-block
Dim i As Integer
Do
End Sub
For...Next Loop
The For...Next loop repeats for known times. This loop uses a variable called as loop's
counter, that increments or decrements in value during each repetition of the loop. The value of this variable
will be the test condition for the loop to repeat or terminate.
Statement-block
Next [counter]
Dim i As Integer
sum = 0
For i = 1 To 50
sum = sum + i
Next
End Sub
For...Next loop:
While...Wend loop:
• Test variable value is read rather than the from the update expression
• Test variable value is read rather than the from the update expression
string:
The string are more flexible and powerful data in VB. the string type data in single, char, fixed length
string & variable length string
Syntax:
Example:
String functions:
1. Left$ function:
This function can return specified number of characters from the left side of string.
Syntax:
Example:
left function:
There is another function left which is more flexible than left$ and returns variant
Syntax:
Example:
right char=left(tittle.12)
Syntax:
Example:
right function:
there is another function which is more flexible than right$ & returns variant
syntax:
right(var_name as string,length as long)
example:
dim right char as string
right char= right(tittle,10)
3. mid$ funtion :
This can return specified no of character from a string. This function is exclusively used
for string
syntax:
mid $(var_name as string,start as long,length)as string
example:
tittle=”VB prog”
dim midchar as string
midchar =mid$(tittle 5,4)
mid function:
There is another function “mid” which is more flexible than “mid$” & returns variants.this
can be used to both numerical & strings
syntax:
mid(var_name as string,start as long[length])
example:
tittle=”VB prog”
dim midchar as string
midchar =mid(tittle 5,4)
The Visual Basic provides many facilities to work with date and time. The functions Date( )and Now( )
are extinsively used to work with Current date and time