0% found this document useful (0 votes)
18 views

VB-Unit-3-Notes

Uploaded by

Narenkumar. N
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

VB-Unit-3-Notes

Uploaded by

Narenkumar. N
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

UNIT III

ARRAY:
 An array is a representation of data in contiguous area in the
memory of our system.
 An array consists of collection of elements of the same type
that are referenced by a common name.
 Each element in the array is distinguished by the index.
 All the elements in an array must be of the same data type.
 Each element of an array is referred by the array name and
the subscript or index.
 LBound and UBound are built in functions which return the
lower and upper bounds of an array.

TYPES OF ARRAY:
There are 3 types of arrays. They are:
 Fixed array
 Dynamic array
 Multidimensional
array
 Fixed array:
A fixed size array always has the same size. The scope of
the array decides how a fixed size array is declared. We can
declare fixed array by:
 Creating a pubic array:
Use the public statement in the declaration section
of the module.

1
 Creating a module-lever array:
Use the private statement in the declaration section of the
module
 Creating a local array for a procedure:
Use the private statement in a procedure.

SYNTAX:
Dim <array_name>(index-size) As <data- type>

Example:
Private sub Form_Load()
Form1.show
Dim a(4) As Integer
Dim num as Integer, counter as Integer
For counter=0 to 4
Num= num+1
a(counter)=num+2
print a(counter)
next
End sub

2
Result:
3
4
5
6
7

 Dynamic array:
 Dynamic arrays are used to change the size
of array at run time.
 You can resize a dynamic array at any time.
It helps you to manage more efficiently.
 After a dynamic array is initially declared you
can add new elements as needed
 You can declare a dynamic array by giving it
an empty dimension list.
Syntax:
Dim <array name >() as <data type>
Example:
Dim A() As Integer
ReDim statement:
 Re Dim (Re dimension) statement is used to resize the
dynamic array at runtime.
 The re dim statement can appear only in a procedure.
Syntax:
Re Dim preserve <array name>(size)

3
Example:
Re Dim preserve
 Multidimensional array:
Multidimensional array are used to keep track of related
information in an array.
Syntax:
Two dimensional array
Dim <array name> (row size,col size) As <data type>
Example:
Dim A(3,3) As integer
CONTROL ARRAY:
 A control array is a group of controls that share the
same name and type.
 Control arrays consume fewer resources as compared
to multiple controls used for the same purpose in a form
at design time.
 The controls also share the same event procedures and
each individual control can have different property
values.

Creating a control array at design time:


During design time, you can create control arrays in
either of the two ways.
1. By assigning the same name to more
than one control.

4
2. By copying an existing control and then
pasting it on the same form.

When more than one control of the same name is created in the
same form, VB displays a control array message box prompting
you want to create a control array. Press yes button to create a
control array.

SEARCHING AND SORTING:


SEARCHING:
 There are basically two methods of searching
through data. The first and most simplistic can be
used on any data whether sorted or not.
 The second requires that the data is sorted and
that random/direct access is possible.
 Here's an outline for a program that searches
through a list of names that is already in
alphabetical order.
1. Divide the list in half.
2. Determine whether you have gone too far.
3. If you have gone too far, look at otherwise look at the second
half.
Go back to step 1 as long as there are names left to look at.
Example:
Private sub Binary search(X$(), Target$)
Dim Low as Integer, High as Integer, Middle as Integer
Target position=0

5
Low = LBound(X$)
High = UBOUND(X$)
Do
Middle = (Low + High)/2
Integer division since using Integers
Select Case X$(Middle)
Case Is = Target$
Target position = Middle
Case Is > Target$
High = Middle-1
Case Is < Target$
Low=Middle +1
End Select
Loop Until Target Position <>0
Print Target Position
End Sub
SORTING:
o Sorting data is one of the most common jobs a
computer do.
o It is also one of the most consuming tasks for a
computer.
o This section discusses four methods.
o The first two are useful for short list.
o The third is often the method of choice, evenfor lists
having thousands of entries.

6
o The last sort method is called the bubble sort and
seems to be sort most commonly given in elementary
books.

RIPPLE SORT:
The first way of sorting card just described is usually called a
ripple sort. Here's an outline for it:-
1. Start with first entry
2. Look at with remaining entries one by one. Whenever
you find a smaller entry, swap it with the first entry, so
that the first entry always holds the candidate for the
smallest entry.
3. Now shrink how you look at the list by starting with the
second entry and look at look at the list by starting with
the second entry and look at the remaining entries (3, 4
and so on).
4. Continue step 3 until all items are worked through.
Example coding:
Private Sub RippleSort(A $ ())
Dim NumOfEntries%, NumOfTimes%, I%, J%
NumOfEntries% = UBound(A$)
NumOfTimes% =NumOfEntries-1
For I% = LBound(A$) To NumOfTimes%
For J% = I% + 1 ToNumOfEntries%
If A$(J%) <S%(I%) Then SWAP A$(J%), A$(I%)
Next J%
Next I%

7
End Sub
Sub SWAP(X As String, Y As String)
Dim Temp As String
Temp = X
X=Y
Y = Temp
End Sub
Big Advantage of this Sort is it moves the object efficiently.
INSERTION SORT:
 The second method, usually called insertion sort is no harder to
program.
 In this sort, at every stage you'll have a sorted, smaller list.
 This leads to a tweak. The new version is even easier to
program.
 Instead of moving forward from the start of the list, move
backward from the end of the list. Each the comparison fails,
move the old entry down by one.
Example coding:
Private sub Insertion sort(A$ () )
Dim NumOfEntries%,I%,J%, Temp$
NumOfEntrie% = UBound(A$)
For I% = 2 ToNumOfEntries%
Temp$ = A$(I%)
For J% = I% -1 to 1 step -1
If Temp$ >= A$(J%) Then Exit For
A$ (J% +1) = A$ (J%)

8
Next J%
As (J% +1)= Temp$
Next I%
End Sub
SHELL SORT:
 The disadvantage of insertion sort is that most of the
time it moves object inefficiently.
 Once the smallest object gets to the top, it stays there
in a sense. Then insertion and ripple sort are opposites.
Example:
Sort the numbers 6,57,3,12,9,1,7,8,2,5,4,97
Example coding:
Private sub ShellSort(A$ ())
Dim NumOfEntries%, I%, J%, Temp%
NumOfEntries% = UBound(A$)
Increm% = NumOfEntries%/2
Do until Increm%1
For I% = Increm%+1 ToNumOfEntries%
Temp$ = A$(I%)
For J% = I% - Increm % to Step -1
If Temp$ >=A$(J%) Then Exit For
A$ (J% +Increm%) = Temp$
Next J%
A$ (J% + Increm%) = Temp$

9
Next I%
Increm% = Increm%/2
Loop
End Sub

BUBBLE SORT:
Bubble sort is a common sort.
Example coding:
Private sub Bubble sort(A$ ())
N= UBound(A$)
For I= 2 To N
For J = N To I Step -1
If A$ (J-1) > A$(J) Then
Temp$ = A$ (J-1)
A$ (J -1) = A$(J)
A$(J) = Temp$
End If
Next J
Next I
End Sub.

10
COMBO BOXES:
VB automatically adds vertical scroll bars whenever the list
box is too small for all the items it contains.

Properties:
List This is a property of both list and combo boxes. This
gives you way to initialise the items in the list or
combo box instead of doing in at run time.
List Count It gives you the number of items on the list in both list
and combo boxes.
List Index The value of the listindex property gives the index
number of the highlighted item, which is where the
user has stopped list.
Text This property of both list and combo boxes and it gives
the currently selected item stored as a string.

GRID CONTROL:
Grid is so important for accurately position controls, mastering it will help
you to give your application a finished, professional look.
Tools Options

11
PROPERTIES:
Show Grid You can turn the grid ON or OFF by changing the
show grid setting. The default setting is ON. There is
usually little reason to grid OFF.
Grid Width, The width and height text boxes let you set the distance
Grid Height (in twips) between grid marks. The default is 120
Boxes twips.
Align Controls The align control to Grid check box determine whether
to Grid controls automatically move to the next grid mark or
whether they can be placed between grid mark.

PROJECTS WITH MULTIPLE FORMS:


Multiple forms will add flexibility and power to your
application. To add more forms to an application you're designing,
Open the project menu and choose Add form . You will get the
add form dialog box. Using this dialog box you can work in
multiple forms.
NAVIGATING AMONG FORMS:
 The project explorer window lists by name all the forms.
 The actual files have the .frm extension.
 VB stores each form a separate .frmfile to keep track of
where they are stored, along with any .frx file that is
needed for the images attached to a form.
 By using project explorer window we can navigate from
one form to another.

12
HANDLING MULTIPLE FORMS AT RUN TIME:
VB displays at most one form when an application starts
running. This is called Startup form. Four methods are available
to work with multiple forms. They are
I. Show
II. Load

13
III. Hide
IV. Unload
SHOW:
The show method is used to show the form on the screen
and it will also move the current form to the top of the desktop if it
was covered by another form.
SYNTAX:
Formname.show
EXAMPLE:
Form1.show
LOAD:
The load method places the form into memory but does not
display it.VB also load the form into memory whenever you refer
to its properties or controls in code.
SYNTAX:
Load FormName
EXAMPLE:
Load Form1

HIDE:
The hide keyword remover the form from the screen but
doesn't unload it from memory. The controls are not available to
the user, but you can still refer them in your code.
SYNTAX:

14
FormName.Hide
EXAMPLE:
Form1.Hide
UNLOAD:
The unload keyword removes the form from memory. Once if
you unload the forms by using unload method then all information
contained in its form-level variables will be lost.
SYNTAX:
Unload FormName
EXAMPLE:
Unload Form1
DO EVENTS AND SUB MAIN:
The do event statements passed control to windows, and VB
gets control back only after the operating system has finished
processing the events in its queue.
EXAMPLE:
Private sub Form_Load()
Show
Do
Do event
Loop
End sub
Do events can actually be used as a function in this case it
returns the number of loaded forms. Sub procedure called main

15
that may be attached to any code module. You can have only one
sub main procedure in any project.
EXAMPLE:
Sub Main()
Frmsplash.Show
Frmsplash.Refresh
Frmmainfrom.Show
End sub

ERROR TRAPPING:
 In VB, while writing code for your application you
first need to trap errors.
 Then from the different options available on how to
handle these errors, choose the one that suites
your requirements.
 The OnError statement is used to handle errors at
runtime.

THE ONERROR STATEMENT:


The OnError statement enables you to trap runtime
errors and specifies where the program execution should
continue when an error occurs at runtime. You need to
place this statement, at the beginning of the program code.

There are three ways how the OnError statement can be used
1. OnErrorGoTo(Line Label1):

16
This statement enables the application to branch
to the line label specified along with the OnErrorGoTo
statement, whenever an error occurs at runtime.

2. OnError Resume Next:


 In a runtime error occurs, this statement
returns the control to the next line in your
program code, where the errors occurs.

 This form of the OnError statement is used


for online error processing.

3. OnErrorGoTo 0:
This statement is used to disable the error handlers in
the current procedure.
If an error occurs at runtime, this error is passed up to
the call stack to determine if an error handler can be found.
If no error error-handler is present then a runtime error
is generated and your program will terminate.

EXAMPLE:
Public Sub Calculate()
OnErrorGoTo Check
Intdiv = num1/num2
Print intdiv
Exit Sub
Check: MsgBox(“RunTime Error-Division by Zero”)
End Sub

Private Sub Form_Load()


Calculate
End Sub

17

You might also like