Académique Documents
Professionnel Documents
Culture Documents
1.Introduction to vb
What is vb?
vb is a user friendly event driven and partly objected oriented
programming language using which we can develop:
database application.
web enabled application.
and variety of other window based application.
Who developed vb?
vb is developed by Microsoft in 1991 as Visual Basic 1.0.
Why vb was developed?
before vb window based application was being done using c and
window based compiler which was a slow and tedious process. it
required lots of coding. to speed up development of window
based application vc++ and Microsoft foundation class were
developed. but the development process was still tedious and
slow. to boost development of window based application easily
and fastly vb was introduced.
What are the different editions of vb?
there are three editions of vb:standard or learning edition: contains basic language feature and
tools.
professional edition: contains more feature and tools when
compared to standard edition.
enterprise edition: contains most advance feature and tools.
the edition which we are using is enterprise edition.
What are the hardware requirement to use vb?
driven
programming
is
not suitable
application.
for
window
basedsuitable
for
application.
window
based
2. Creating programs
What is Object?
object is class variable which is composition of properties,
methods and data member. object is fundamental requirement of
any object oriented language. object has three characteristics:
state: state of any object it defines identity of object.
behaviour: behaviour of any object is decided by member
functions(methods) to which object can respond.
method: it is also known as member function it performs
computation on data member or any kind of action.
What is property?
property of any object is actually member procedure which
allows to set value in data member ( working as mutator) or
allows to retrieve value of data member ( working as accessor).
How to create program in vb?
steps are as follows:
first decide the controls which will be used to create graphical
user interface.
set properties of controls
write event procedure coding.
How to get print of visual basic program.
fundamental
requirement
of
any
programming
Range
Memory
1.Byte
0 , 255
2.Integer %
32767 , 0 , -32768
3.Single !
4.Double #
5.Date
1-1-100 to 31-12-9999 8
6.string(variable) $
2 billion characters
6.string(fixed) $
7.boolean
True, false
8.object
9. variant
string is
2
billion
c=10\3 c will be 3
c will be 3.333
=
4 .Controlling program Execution
Why do we need control structure?
We all need to alter our action depending on changing
circumstances. For example If weather is fine I will go
Dongargarh on bike otherwise I will go by train. In the same way
vb language too must be able to perform different action under
one condition and different action under another condition and
this is made possible by control structure. Sometimes a given set
of statements are needed to run repeatedly this is done using
loop control structure.
Write notes on if control structure./ explain branching
structure in vb./ write short notes on if statement/ write
short note on select case.. end select.
The general form of if statement is:
(a)if-endif statement
the general format of if statement is
if (condition is true) then
execute this statement
end if
The keyword if tells the compiler that what follows, is a decision
control instruction. The condition following the keyword if is
optionally enclosed within a pair of parentheses. If the condition,
whatever it is, is true, then the statement is executed. If the
condition is not true then the statement is not executed; instead
the program skips past it.
(b) if else-endif statement
if statement by itself can execute only one statement if condition
is true. If it required running a group of statements when
condition is true we have to enclose those statements
inside curly brace known as compound statement. The
above form of if statement mentioned in (a) will not do anything
when condition is false. If we want to run statement when
condition is false we need if-else construct.
General format of if-else construct is
If (condition is true) then
else
do this
do this
end if
3. if (condition) then single-true statement
4. if (condition) then single-true statement else single-falsestatement
5. if (condition) then
if (condition) then
statement
else
statement
end if
else
if (condition) then
statements
else
statements
end if
end if
if condition then
statements
elseif condition then
statements
elseif condition then
statements
else
statements
end if
program to find division given percentage
Private Sub Command1_Click()
Dim a!, b$
a = Text1.Text
If a < 33 Then
b = "fail"
Else
If a < 45 Then
b = "third"
Else
If a < 60 Then
b = "second"
Else
b = "first"
End If
End If
End If
Text2 = b
End Sub
place label : having caption enter percentage
place text1: having text property cleared
place label2 : having caption division
place text2: having text property cleared
place command1: having caption find division
program to check leap year
Private Sub Command1_Click()
Dim a%
a = Text1
If a Mod 100 = 0 Then
If a Mod 400 = 0 Then
Text2 = "leap"
Else
Text2 = "not leap"
End If
else
If a Mod 4 = 0 Then
Text2 = "leap"
Else
Text2 = "not leap"
End If
End If
End Sub
place label : having caption enter year
place text1: having text property cleared
place label2 : having caption result of testing
place text2: having text property cleared
place command1: having caption check for leap year
if-else
Multiple
ifNested
if..else
if
requires
less
Therefore
it
is
(entry
level
control
short
notes
on
while
(entry
level
control
and do-while
Body of loop in case of while loop will not execute even once if
condition of while loop is false but in case of do-while loop body
of loop will run at least once because condition is tested at the
end of loop body.
format of do..loop is
initialize loop counter
do
do this
increment loop counter
loop while(condition is true)
t=t&""&n
End If
Next
Text1 = t
End Sub
place label1 : having caption set to prime series is
place text1: having text property cleared
place command1: having caption print series in text box
Define Array.
Array or Subscripted variables:
Array is collection of variables having common name and
common data type. Individual variable in collection is identified
by index or subscript. Elements of array occupy contiguous
memory location.
How can we declare a single dimension Array?
Declaration (single dimension numeric array):
dim a(5) as integer
or
dim a%(5)
a is name of array.
it means there are 6 variables viz. a(0),a(1),a(2),a(3),a(4),a(5)
index or subscript will ange from 0 to 5. All variables will be
integers.
dim b!(10)
or
dim b(10) as single
It means there are 11 variables viz. b(0),b(1) b(5) index or
subscript will change from 0 to 10. All variables will be single.
How we can initalise an array?
Initialization of array is not supported by vb.
For i = 0 To 2
For j = 0 To 2
c(i, j) = a(i, j) + b(i, j)
Next
Next
End Sub
Private Sub Command3_Click()
For i = 0 To 2
For j = 0 To 2
Print c(i, j) & " ";
Next
Print
Next
End Sub
Program to sort a given array
place three command buttons on form labeled input,add,print
Dim a%(4)
Private Sub Command1_Click()
Dim i%
For i = 0 To 4
a(i) = InputBox("enter value", "data entry", 1)
Next
End Sub
Private Sub Command2_Click()
Dim i%, j%
For i = 0 To 3
For j = 0 To 3 - i
If a(j) > a(j + 1) Then
t = a(j)
a(j) = a(j + 1)
a(j + 1) = t
End If
Next
Next
End Sub
Private Sub Command3_Click()
Dim i%
For i = 0 To 4
Print a(i)
Next
End Sub
What is dynamic array?
dynamic array is an array whose no. of elements are not
specified earlier. no. of elements can be requested from user at
program run time. redim keyword can be used to allocate
memory for such no. of elements. after allocating memory we
can store value in elements of array.
dynamic array saves memory because we have to allocate
memory for such many elements which we want to use.
Program to sort an array using dynamic array
Dim a%(), n 'note element is not specified
Private Sub Command1_Click()
Dim i%
' take no. of elements required
n = InputBox("enter no. of elements")
'allocate memory
ReDim a(n - 1) ' workout
For i = LBound(a) To UBound(a)
a(i) = InputBox("enter value", "data entry", 1)
Next
End Sub
Private Sub Command2_Click()
Dim i%, j%
For i = LBound(a) To UBound(a) - 1
For j = LBound(a) To UBound(a) - 1 - i
If a(j) > a(j + 1) Then
t = a(j)
a(j) = a(j + 1)
a(j + 1) = t
End If
Next
Next
End Sub
Private Sub Command3_Click()
Dim i%
For i = LBound(a) To UBound(a)
Print a(i)
Next
End Sub
place command1:having caption set to input
place command1:having caption set to sort
place command1:having caption set to print
Program to add two matrix using non-dynamic array.
Dim a%(2, 2), b%(2, 2), c%(2, 2), i%, j%,t$
Private Sub Command1_Click()
MsgBox "enter value for array a"
For i = 0 To 2
For j = 0 To 2
a(i, j) = InputBox("enter value")
Next
Next
MsgBox "enter value for array b"
For i = 0 To 2
For j = 0 To 2
If n <> p Then
MsgBox "invalid matrix order for multiplication"
Exit Sub ' come out of event
End If
MsgBox "enter value" & m * n & " for array a"
ReDim a(m - 1, n - 1) 'allocate memory before storing value
For i = LBound(a, 1) To UBound(a, 1)
For j = LBound(a, 2) To UBound(a, 2)
a(i, j) = InputBox("enter value")
Next
Next
MsgBox "enter value" & p * q & " for array b"
ReDim b(p - 1, q - 1) 'allocate memory before storing value
For i = LBound(b, 1) To UBound(b, 1)
For j = LBound(b, 2) To UBound(b, 2)
b(i, j) = InputBox("enter value")
Next
Next
End Sub
Private Sub Command2_Click()
ReDim c(m - 1, q - 1)
For i = LBound(a, 1) To UBound(a, 1)
For k = LBound(b, 2) To UBound(b, 2)
c(i, k) = 0
For j = LBound(a, 2) To UBound(a, 2)
c(i, k) = c(i, k) + a(i, j) * b(j, k)
Next
Next
Next
End Sub
Private Sub Command3_Click()
For i = LBound(c, 1) To UBound(c, 1)
a(0) = 10
a(1) = 20
a(2) = 30
Print a(0), a(1), a(2)
'change array size and use preserve
ReDim Preserve a(3)
a(3) = 40
Print a(0), a(1), a(2), a(3)
End Sub
place command1:having caption set to test without preserve
place command1:having caption set to test with preserve
What is function?
a number of statements grouped into a single logical unit is
referred to as a function which returns a value and which is made
to complete a specific task. A program can be made of many
functions.
Types of functions:
User defined function: these are those functions which are
made by programmer for his programming convenience.
Library function: these are those functions which are
readymade and are provided by compiler, uses of which are
possible by including corresponding header files. Source code of
library function is not available to programmer but its object
codes are available in precompiled form.
What is procedure?
a number of statements grouped into a single logical unit is
referred to as a procedure or subroutine which does not return
any value and which is made to complete a specific task. A
program can be made of many procedures.
What
are
the
functions/procedures?
Usages are as follows:
usages/applications
of
Function/procedure
avoids
code
repetition:once
a
function/procedure has been made we can call the
function/procedure to complete the task when and where
necessary.
Function/procedure used carefully makes the process of error
removing easier called debugging.
Once function/procedure is tested and satisfies the ones needs,
one can convert it in to library, and can distribute it others for
use.
Function/procedure allows breaking bigger task into smaller
manageable subtasks which is soul of modular programming.
Recursive function/procedure (function/procedure calling itself at
least once) solves some typical programming tasks easily and
with few lines of coding which otherwise would have taken
several lines of code.
End Sub
Private Function fact&(n As Integer)
If n <= 1 Then
fact = 1
Else
fact = n * fact(n - 1)
End If
user interface:
create a command button: having caption find factorial
create text1: having text property clear
create text2: having text property clear
create label1: having caption property set to enter a value
create label2: having caption property set to factorial is
write program to find factorial using function.
Private Sub Command1_Click()
Dim n%, f&
n = Text1
f = fact(n)
Text2 = f
End Sub
'Private Function fact&(byref x%)
reference
'Dim i%, f&
'f = 1
'For i = 1 To x
'f=f*i
'Next
'fact = f
'End Function
Private Function fact&(ByVal x%)
Dim i%, f&
f=1
For i = 1 To x
by default calling by
f=f*i
Next
fact = f
End Function
user interface:
create a command button: having caption find factorial
create text1: having text property clear
create text2: having text property clear
create label1: having caption property set to enter a value
create label2: having caption property set to factorial is
Explain call by value and call by reference
'when called routine is not able to change value of actual
argument
'through dummy argument it is known as call by value
Private Sub Command1_Click()
Dim a%, b%
a = Text1
b = Text2
swap a, b
'after call
Text5 = a
Text6 = b
End Sub
Private Sub swap(ByVal a%, ByVal b%)
'default is call by reference
Text3 = a
Text4 = b
Dim temp%
'interchange
temp = a
a=b
b = temp
End Sub
call by reference:
'when called routine is able to change value of actual argument
'through dummy argument it is known as call by reference
Private Sub Command1_Click()
Dim a%, b%
a = Text1
b = Text2
swap a, b
'after call
Text5 = a
Text6 = b
End Sub
Private Sub swap(Byref a%, Byref b%)
'default is call by reference
Text3 = a
Text4 = b
Dim temp%
'interchange
temp = a
a=b
b = temp
End Sub
user interface:
6 textboxes: having text property clear and having default name
text1,text2
6 labels: having caption enter value of a, enter value of b, within
called subroutine value of a, within called subroutine value of b,
after call to swap value of a, after call to swap value of b
1 command button: having caption swap and having default
name command1