[go: up one dir, main page]

0% found this document useful (0 votes)
6 views26 pages

VB UNIT - III Part - I

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views26 pages

VB UNIT - III Part - I

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

UNIT – III

VISUAL PROGRAMMING

What is variable? How we declare a variable Explain with example.

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

 Syntax for Declaring Variable

Dim VaribaleName As DataType

Example :

Dim Regno as integer

Dim price as single

Dim myname as string.

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.

Data types in Visual Basic are as follows


1. Numeric

2. String

3. Date

4. Boolean

5. Variant

1. Numeric :

i) Byte : Store integer values in the range of 0-255

example:

dim a as Byte

Visual Programming - I B.C.A Semester III: Visual Programming 1


ii) Integer : This Data Type is used to create variable that are store whole Numbers. It reserve 2 Byte of
Memory.This Store integer values in the range of (-32,768) to (+39,76 7)

example:

Dim Regno as integer

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

Visual Programming - I B.C.A Semester III: Visual Programming 2


3. Date :

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

Visual Programming - I B.C.A Semester III: Visual Programming 3


Scope of the variable:

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

Visual Programming - I B.C.A Semester III: Visual Programming 4


Operator used in VB
Operator is a symbol used to indicate certain operation on data associated with it. The data on
which operator is called operand.

Different groups of operator used for different purposes is listed here


1. Arithmetic operators
2. Comparison or relational operators
3. Logical operators
4. Bitwise operator

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

The VB provides another set of operators called as relational or comparative operators.


The result of such operation is two valued either true or false (1 or 0)

Visual Programming - I B.C.A Semester III: Visual Programming 5


3. Logical 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

Visual Programming - I B.C.A Semester III: Visual Programming 6


Visual Programming - I B.C.A Semester III: Visual Programming 7
Visual Programming - I B.C.A Semester III: Visual Programming 8
Selection Structures (or Branching Statements)
The Visual Basic has If...Then, If... Then...Else and Select Case three decision making
structures. Some times these statements are called by the name branch ing statements. If... Then Selection
Structure (or Statement)

 If... Then structure


If... Then structure tests the condition specified, when condition is true, executes the statement(s)
that follow, when condition is false, it continues with the statement following the if then structure.

syntax :

If condition Then

Statement

End If

example :

If Marks < 35 Then

Print "Result is failed"

End If

Flow chart

Visual Programming - I B.C.A Semester III: Visual Programming 9


 If... Then...Else Selection Structure (or Statement)
It is a variation of If...Then control structure. It tests a condition, when condition is true
it executes a statement or a set of statements in Then block. When condition is false executes statement or
set of statements in the else block.

The syntax of If... Then...Else statement is given below.

If condition Then

Statement-block1

Else

Statement-block2

End If

example :

If Marks < 35 Then

Print “Result is Failed”

Else

Print “Result is Passed”

End If

Flow chart

Visual Programming - I B.C.A Semester III: Visual Programming 10


 If... Then...ElseIf...Else Selection Structure (or Statement)
It is a variation of the If... Then ...Else statement, which uses several conditions with the Elself clause
in between If... Then and Else blocks. You can have any number of Elself clauses in If... Then ... Else
structure.

The syntax is as follows.

If condition1 Then

Statement-block1

ElseIf condition2 Then

Statement-block2

ElseIf condition3 Then

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 :

If Marks < 35 Then


Print Result is failed"
ElseIf Marks <60 Then
Print Result is passed"
ElseIf Marks < 70 Then
Print "Result is First Class"
Else
Print "Result is Distinction “
End If

Visual Programming - I B.C.A Semester III: Visual Programming 11


Nested Control structures
The Visual Basic allows using one control structure inside the same or different control structure,
then the program structure looks like nested. This facility therefore often termed as nesting the control
structures.

 Nested If Selection Structure (or Statement)

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

Visual Programming - I B.C.A Semester III: Visual Programming 12


 Select...Case Control Structure
The Select... Case structure tests a single expression once at the top of the structure. The
result of the test is then compared with several values and if the result matches one o them, the
corresponding block of statements is executed.

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.

The general syntax is

Select case expression

case value_1:

statement1;

case value_2:

statement2;

case value_n:

statement n;

Case Else :

statement x;

End Select

Visual Programming - I B.C.A Semester III: Visual Programming 13


example :

Select Case opr

case '+' : result = a + b;

case '-' : result = a - b;

case '/' : result = a / b;

case '*! : result = a* b;

Case Else: print "Error: is not a valid operator"

End select

Flow chart

Visual Programming - I B.C.A Semester III: Visual Programming 14


IIF function
This function is an equivalent code to using If…Then...Else structure. This function returns one of
two parts, depending on the evaluation of an expression.

The syntax is :

Function IIF(Expression, TruePart, FalsePart)

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.

The following line of code returns the bigger of two values.

Big = IIf(vall > val2, vall, val2)

Visual Programming - I B.C.A Semester III: Visual Programming 15


Repetition Structures (or Looping Statements)
Repetition structures allow executing one or more lines of code repetitively. m many
applications, the several tasks involved are repetitive in nature, and therefore repetitive sauce tures are
important part of any programming language. The Visual Basic supports Tonowng six types of repetitive
structures.

1. While... Wend

2. Do...Loops

 Do...while...Loop
 Do...Until...Loop
 Do...Loop...While
 Do...loop...Until

3. For... Next

While ... Wend Repetition Structure


This is the simplest repetition structure among all. While...Wend repetition structure
may have one or more statements in its body. The while...wend repeats the loop as long as the condition
is true. When condition becomes false, the loop exits and control transfers to the very next statement
following the Wend clause.

The syntax is as follows.

While Condition

statement-block

Wend

Visual Programming - I B.C.A Semester III: Visual Programming 16


Example :

Private Sub PrintNums_Click().

Dim i As Integer

While i <= 10

Print i & Space (2) ;;

i=i+1

Wend

End Sub

Do... While... Loop


In the Do... While... Loop, the loop is executed while the condition is true, the key words
while specify the condition how long the loop statements will be executed.

syntax :

Do While Condition

Statement-block

Loop

Visual Programming - I B.C.A Semester III: Visual Programming 17


Example:

Private Sub PrintNums_Click().

Dim i As Integer

Do While i <= 10

Print i & Space (2);

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

Visual Programming - I B.C.A Semester III: Visual Programming 18


Private Sub PrintNums_Click()

Dim i As Integer

Do Until i > 10

Print i & Space (2);

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

Loop While Condition

Visual Programming - I B.C.A Semester III: Visual Programming 19


Example:

Private Sub PrintNums_Click()

Dim i As Integer

Do

Print i & Space (2);

i=i+1

Loop While i <= 10

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

Loop until condition

Visual Programming - I B.C.A Semester III: Visual Programming 20


Example:

Private Sub Print Nums_Click().

Dim i As Integer

Do

Print i & Space (2);

Loop Until i > 10

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.

The general syntax :

For counter = start to end [step increment]

Statement-block

Next [counter]

Visual Programming - I B.C.A Semester III: Visual Programming 21


Private Sub FindSum_Click()

Dim sum As Long

Dim i As Integer

sum = 0

For i = 1 To 50

sum = sum + i

Next

Print "The Sum is : " & CStr (sum)

End Sub

Comparison of All Loops

For...Next loop:

· Predetermined times of repetitions.

• Test variable is loop variable

While...Wend loop:

• Test variable need not be loop variable.

• Terminal count need not be known

• Test variable value is read rather than the from the update expression

Do... While... Loop and Do...Until...Loop loops:

· Test variable need not be loop variable.

• Terminal count need not be known

• Test variable value is read rather than the from the update expression

Do...Loop...While and Do...Loop...Until loops:

. Loop body to be executed at least once

· Other features are similar while

Visual Programming - I B.C.A Semester III: Visual Programming 22


String functions

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:

dim var _name as string

 Example:

Dim tittle as string


tittle =”visual basic programming”

String functions:

1. Left$ function:

This function can return specified number of characters from the left side of string.

 Syntax:

left $(var_name as string, length as long)

 Example:

dim left char as string

left char=”left $(tittle.12)

left function:

There is another function left which is more flexible than left$ and returns variant

 Syntax:

left(var_name as string,length as long)

 Example:

dim left char as string

right char=left(tittle.12)

Visual Programming - I B.C.A Semester III: Visual Programming 23


2. right$ function:
This function returns specified number of char from right side of a string

Syntax:

right$(var_name as string, length as long) as string

Example:

dim right char as string

right char= right $(tittle,10) as string)

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)

Visual Programming - I B.C.A Semester III: Visual Programming 24


Data and Time function

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

The following Operations can be done on date


• You can substract or add two dałes directly, the result is not in date format rather total days.
• You can add days to a date variable
• You can find year, month and day of the date.
The function Day, Week Day, week Day Name, Month you are used to extract Corresponding
Month Name, and you are used to extract a Information from the date or date variable used as
Argument to them.
The function Timer is extensively used to work with Current time, you can find hours, minutes &
Seconds of the time part in the date variable Using functions hours,
Minutes, and seconds.
Example :-
Private Sub MONTH DTS_click( )
Dim mydate As Date
mydate = Date( Text1.text)
Print "Month: “ & month (mydate)
Print "Month Name:" & month name ( month( mydate))
End Sub

Visual Programming - I B.C.A Semester III: Visual Programming 25


Module AND Types of Modules
The VB Modules are the development tools used to encapsulate the code to reuse it later
to develop applications .
The VB basically has the following code to capsulating tools or Modules
 Standard or code Modules
 Class Modules
 Form Modules
1. Standarad Modules : (file names with extention) are the containers for procedurews and
declarations commonly accessed by other modules within the applications . they can
containglobal or modules level declaration of variables,constants,types,external
procedures, and global procedures(defined in module).
2. Class module:file names with extention.cls)are the foundation of object oriented
programming in VB you can write code in class modules to create new objects.these
objects can include your own customized properties and methods.these can be reused
later by any one by referencing those library.
3. Form modules: the form modules are code encapsulating tools used in the design and
development of forms or in other words forms are a special type of module can be
present in the project the form modules cosists pieces of program segments called as
procedure.

Visual Programming - I B.C.A Semester III: Visual Programming 26

You might also like