0% found this document useful (0 votes)
126 views8 pages

VB Language Lab

This document provides instructions for exercises demonstrating new features in Visual Basic 7 including inheritance, method overriding, free-threading support, structured exception handling, parameterized constructors, method overloading, and interface declarations. The exercises use a demonstration application containing a form for launching background tasks. Exercise 1 examines the use of interfaces by stepping through code implementing a FractalForm class that inherits from a Task interface. Exercise 2 demonstrates free-threading by stepping through the creation of a new background thread to run a fractal generation algorithm. Exercise 3 examines inheritance by reviewing code for a Queue class and a derived DynamicQueue class.

Uploaded by

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

VB Language Lab

This document provides instructions for exercises demonstrating new features in Visual Basic 7 including inheritance, method overriding, free-threading support, structured exception handling, parameterized constructors, method overloading, and interface declarations. The exercises use a demonstration application containing a form for launching background tasks. Exercise 1 examines the use of interfaces by stepping through code implementing a FractalForm class that inherits from a Task interface. Exercise 2 demonstrates free-threading by stepping through the creation of a new background thread to run a fractal generation algorithm. Exercise 3 examines inheritance by reviewing code for a Queue class and a derived DynamicQueue class.

Uploaded by

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

Visual Basic Lab: Demonstration

of New Features in the VB7


Language
Estimated time to complete all exercises in this lab: 60 minutes.
Objectives
The objective of these lab exercises is to familiarize the user with the most significant
new features in the VB7 language. n particular the exercises demonstrate the following
features:
nheritance
!ethod "verriding
#ree$threading %upport
%tructured Exception &andling
'arameterized (onstructors
!ethod "verloading
nterface )eclarations
Demonstration !!lication
Each of the lab exercises uses a single demonstration application. The application
contains a *in#orm that allows users to launch tas+s that perform various functions.
Each tas+ that is launched is run in a new bac+ground thread. The form displa,s status
information for the tas+s that are currentl, running- including the percent completion of
the tas+. *hen a tas+ is completed it is removed from the list. #our t,pes of
bac+ground tas+s are supported:
#ractal#orm that generates a fractal image and displa,s it in a form
.ueue Tester that tests a /ueue implementation
%orted0rra, that tests a class which builds a sorted arra,
.%ort which uses the .uic+$%ort algorithm to sort an arra,
0 context menu is supported on tas+ items that allows tas+s to be suspended and
resumed as well as allowing the priorit, of tas+s to be modified.
"#ercise $: %sing &nterfaces in VB7
Estimated time to complete this exercise: $0 minutes.
n this exercise ,ou will examine and step through VB7 code that illustrates the use of
interfaces.
O!en the lab e#ercise !roject
VB Lab: VB Language
1. #rom the d:2VB')(2VB73ang)emo subdirector,- open VB7 3anguage
)emonstration.sln. 4"n lab machines- there should be a shortcut to this
project on the des+top5.
'he 'as( interface is use) for controlling tas(s*
6. )ouble clic+ the Tas+s.vb file in the solution explorer. This file contains an
interface definition- Tas+. The Tas+ interface abstracts a set of properties-
methods- and events that are used b, a client form which supports
launching new tas+s and displa,s the status of running tas+s.
Tas+.%tartTas+ is used to begin execution of a tas+.
'he FractalForm class im!lements the 'as( &nterface
7. "pen file #ractal#orm.vb which defines class #ractal#orm. 8ote that the
second statement of this class is 9mplements Tas+9 which specifies that
the class implements the Tas+ interface.
:. %croll down through the file past the nested class #ractal(olors and past
the data member declarations for #ractal#orm to the #ractal#orm
constructor 4%ub 8ew5. (lic+ in the left margin b, the declaration
statement 9%ub 8ew4B,Val height 0s nteger ;59 to set a brea+point on
that statement. 0lso set a brea+point on the declaration line of the
#ractal(alc method 4%ub #ractal(alc ;5 which is the next procedure in
the file.
+tart e#ecution an) initiate a Fractal tas(
<. 'ress #<.
=. (lic+ the 93aunch #ractal9 button in the form. This will hit the first
brea+point that ,ou set in a previous step
+te! through the creation of the Fractal tas(
7. 'ress %hift$#11 to step out of the constructor of #ractal#orm and bac+
into the caller.
>. 'ress #11 to step into the 3aunchTas+ procedure. 8ote that this method
is passed the newl, created #ractal#orm instance but that the argument
t,pe of 3aunchTas+ is of t,pe Tas+ rather than #ractal#orm. The
3aunchTas+ procedure can operate on an, object that implements the
Tas+ interface. #irst 3aunchTas+ creates a new instance of the Thread
class.
?. 'ress #11 three times. (ontrol is now transferred to the implementation
of the Thread propert, provided in the #ractal#orm class. 8ote that the
declaration statement for the Thread propert, ends with 9mplements
Tas+.Thread9. This directs the compiler to use this propert, as the
implementation of the Tas+ interface@s Thread propert,.
1A. 'ress #11 three times which will step through setting the thread propert,
and bac+ into the 3aunchTas+ method.
11. 'ress #11 to execute the 0dd&andler statement which hoo+s up the
Tas+(ompleted procedure of the form class to the 9"nTas+(omplete9
event of the Tas+ interface. *hen the object that implements the Tas+
interface raises the "nTas+(omplete event then Tas+(ompleted will be
called.
+te! through the e#ecution of the Fractal tas(
2
VB Lab: VB Language
16. 'ress #< to continue executionB the brea+point on the #ractal(alc method
of #ractal#orm will be hit.
17. 8ote that the declaration statement of #ractal(alc ends with 9mplements
Tas+.%tartTas+9. This is a case where the name of the procedure is
different than the interface procedure that it implements. There is no
re/uirement that these names match.
1:. 3oo+ at the top of the #ractal#orm class and note the Event statement
that is the third statement within the bod, of the definition of the
#ractal#orm class. This declaration is the same as a normal VB event
statement with the addition that it ends with 9mplements
Tas+."nTas+(omplete9. This specifies that the "nTas+(omplete event of
this class corresponds to the "nTas+(omplete event of the tas+ interface.
1<. %croll down to the end of the #ractal(alc method and place a brea+point
on the CaiseEvent statement that raises the "nTas+(omplete event.
1=. 'ress #<. %witch bac+ to the Tas+s form which will displa, the status of
the #ractal(alc tas+ as it executes. *hen the wor+ of the tas+ is
completed the brea+point on the CaiseEvent statement will be hit.
17. %tep into the CaiseEvent statement using #11 and note that the
Tas+(ompleted procedure within the form is invo+ed. This was the
procedure that the 0dd&andler statement in 3aunchTas+ hoo+ed up to
the "nTas+(omplete event of the Tas+ interface.
Finish e#ecution
1>. 'ress #< and the Tas+(ompleted method will remove the 3isttem for the
tas+ from the 3istView control
"#ercise ,: Free-'hrea)ing
Estimated time to complete this exercise: $. minutes.
In this walkthrough you will examine and step through VB7 code that illustrates
the use of threads.
O!en the lab e#ercise !roject
1. f VB7 3anguage )emonstration.sln is not alread, open- then open it
from the d:2VB')(2VB73ang)emo subdirector,.
+et a brea(!oint within the co)e
6. Cight$clic+ on 0ctiveTas+s#orm.vb in the solution explorer and select View
(ode.
7. %croll down to the 3aunchTas+ method that is the last procedure in the
file. 'lace a brea+point on the first statement of the 3aunchTas+ method
b, clic+ing in the left margin of this line.
Begin e#ecution an) create an instance of FractalForm
:. 'ress #< to begin execution. 0 window will come up with buttons for
launching tas+s on bac+ground threads and a listview control- which
displa,s the status of running threads. (lic+ on the 93aunch #ractal9
button on the form. Execution will stop at the brea+point.
<. 'ress #? to clear the brea+point
3
VB Lab: VB Language
+te! through creation of a new threa)
=. The first statement of 3aunchTas+ creates a new instance of the Thread
class. The Thread class provides an abstraction on a s,stem thread.
*hen the thread is created it is passed a reference to the method that
will be executed on the bac+ground thread. n this case the Thread
constructor is passed a reference to the implementation of the %tart
method of the Tas+ interface that is provided b, the #ractal#orm class.
The start method runs the #ractal generation algorithm.
7. 'ress #1A to step through each statement in 3aunchTas+ as it initializes
communication between the form and the new tas+. The last step of
3aunchTas+ calls the %tart method of the Thread class. This begins
execution of #ractal generator on the bac+ground thread. )o not step
past the End %ub statement.
"#amine the use of s/nchroni0ation !rimitives
>. %croll up to the procedure DpdateTas+3ist that precedes 3aunchTas+.
DpdateTas+3ist is called on a timer event ever, second to update the
displa, of active tas+s. 0 #or Each loop is used to iterate over the tas+
list items and update the displa, for each one. 8ote that this loop is
contained within a %,nc3oc+ E End %,nc3oc+ bloc+. This is used to ensure
that no other thread modifies the contents of the 3istView@s 3isttems
collection while the collection is being wal+ed. 8ote that the expression
specified on the %,nc3oc+ statement specifies the 3isttems collection as
the object on which the loc+ is ta+en.
?. %croll up again to the procedure Tas+(ompleted. This procedure handles
the "nTas+(ompleted event that is raised b, a tas+ when it finishes
executing. Tas+(ompleted wal+s the 3istView@s 3isttem collection to find
the 3isttem that corresponds to the tas+ that has finished. *hen it finds
the correct 3isttem it is removed from the collection. Because
Tas+(ompleted is called from a tas+ object- it will execute on the same
thread that the tas+ is running on. The deletion of the 3isttem from the
3isttem@s collection ma, conflict with the DpdateTas+3ist method running
on the form@s thread. DpdateTas+3ist and Tas+(ompleted use %,nc3oc+ E
End %,nc3oc+ bloc+s to avoid this potential conflict.
1un the a!!lication an) mo)if/ the attributes of running threa)s
1A. 'ress #< and switch bac+ to the Tas+s window.
11. 3aunch three #ractal tas+s and move their windows so the, can all be
viewed at the same time. 8ote that the, will generate their fractal
images in parallel.
"#ercise 2: &nheritance
Estimated time to complete thisexercise: $. minutes.
In this walkthrough you will examine and step through VB7 code that illustrates
the use of inheritance.
O!en the lab e#ercise !roject
1. f VB7 3anguage )emonstration.sln is not alread, open- then open it
from the d:2VB')(2VB73ang)emo subdirector,.
4
VB Lab: VB Language
"#amine the 3ueue class an) the )erive) class D/namic3ueue
6. n %olution Explorer- double$clic+ .ueue.vb to open it in the code editor.
The first class in the file- .ueue- provides a simple implementation of a
/ueue using an arra, to store the /ueue elements. The member variable
mFi#ront stores the index of the element at the head of the /ueue and
member variable mFtem(ount stores the number of elements in the
/ueue. The second element of the /ueue is at location mFi#ront G 1- the
third element is at location mFi#ront G 6- and so on. ndexes into the
arra, are adjusted b, using the !od operator to wrap indices that are
greater than the arra, size bac+ to the beginning of the arra,.
7. %croll down and examine the 0dd and Cemove methods. The 0dd method
of .ueue throws an exception if the /ueue is full otherwise it increments
the count of elements and places the new entr, at the end of the /ueue.
The Cemove function raises an exception if the /ueue is empt, otherwise
it retrieves the element from the front of the /ueue- increments the
index of the first element and decrements the count of elements.
Exceptions are described b, instances of the Exception class or b,
instances of classes that derive from the Exception class. The .ueue
class uses instances of the Exception class created with uni/ue messages
that describe the exception that was encountered.
:. 8ote that the declaration statements of both the 0dd and Cemove
methods contain the +e,word 9"verridable9B this allows each method@s
implementation to be replaced with a new implementation in classes that
derive from the .ueue class.
<. %croll down and examine class ),namic.ueue. The first statement of
),namic.ueue is 9nherits .ueue9 which declares that the
),namic.ueue class inherits the implementation of the .ueue class.
),namic.ueue extends the .ueue class b, allowing the /ueue to grow
as needed. To enable this behavior ),namic.ueue provides its own
implementation of the 0dd method that will grow the arra, in which
elements of the /ueue are stored whenever the arra, becomes full. 'lace
a brea+point on the 0dd method b, clic+ing in the left margin of the first
statement in the method. 8ote that the declaration contains the
9"verrides9 +e,word that serves to ma+e explicit the fact that this
implementation replaces the implementation of 0dd inherited from the
base class.
+te! through the Overri))en )) metho)
=. 'ress #< then clic+ on the 93aunch .ueue Tester9 button on the form.
Execution will stop at the brea+point in the 0dd method of
),namic.ueue.
7. 'ress #1A to step through the 0dd method. The Tr, statement sets up an
exception handler in case the call to the base class@s 0dd method
generates an error. 8o exception is generated on this first call to 0dd.
>. 'ress #< and execution will brea+ again in the 0dd method.
?. 'ress #? to remove the brea+point.
1A. Dse #11 to step through the 0dd method and into the base class@s
implementation of 0dd.
11. Dse #1A to step through the base class implementation. #or the sa+e of
illustration the arra, of the underl,ing .ueue class is initiall, created with
5
VB Lab: VB Language
onl, a single element. 0s a result this call to the base class@s 0dd method
will fail. *hen the Throw statement is executed- control is transferred to
the (atch bloc+ in ),namic.ueue@s 0dd method. The (atch bloc+ calls
Cesize.ueue0rra, which replaces the arra, with a larger one. This is
followed b, a second call to !,Base.0dd to re$add the new element.
16. (ontinue to press the #1A +e, to step out of the 0dd method into its
caller- the CunTest method of the .ueueTester class.
"#amine the im!lementation of the 4ust&nherit 3ueue'ester class
17. The .ueueTester class tests a /ueue implementation b, randoml, adding
and removing items from a /ueue and randoml, calling their (hec+%tate
method. The core of the implementation is the CunTest method- which
performs the various operations on the /ueue. 8ote that the 0dd method
that was just executed is called with an object returned b, the
(reateElement method. This method is defined immediatel, above the
CunTest method. 8ote that (reateElement is declared with the
!ust"verride +e,word and specifies no implementation for the method.
The .ueueTester class is designed for testing an, t,pe of class that
inherits from class .ueue including classes that re/uire specific element
t,pes. t isnHt possible to provide an implementation of (reateElement
that would wor+ for all such classes and so the implementation must be
provided b, a class that inherits from .ueueTester. Because .ueueTester
contains a !ust"verride method the class itself is declared with the
!ustnherit +e,word since the class must be inherited from in order to be
used.
+te! through the e#ecution of 3ueue'ester an) &nteger3ueue'ester
1:. 'ress #1A to step through CunTest. The next operation performed on the
/ueue will be to remove an element. (ontinue pressing #1A until another
call to the 0dd method is about to be made.
1<. 'ress #11. (ontrol is then transferred to the override of the
(reateElement method in the nteger.ueueTester class. This is a simple
class that inherits the .ueueTester class and overrides the (reateElement
method with an implementation that returns random integers.
1=. 'ress #< and switch to the 0ctiveTas+s form. Eventuall, the
nteger.ueueTester will complete the prescribed number of operations
and the tas+ will be completed.
"#ercise 5: Function 6ointers an)
Overloa)ing
Estimated time to complete this exercise: $. minutes.
In this exercise you will examine and step through VB7 code that illustrates the
use of delegates and overloading.
O!en the lab e#ercise !roject an) containe) source file
1. f VB7 3anguage )emonstration.sln is not alread, open- then open it
from the d:2VB')(2VB73ang)emo subdirector,.
6
VB Lab: VB Language
6. )ouble clic+ the %orted0rra,.vb file in the solution explorer. This file
contains two class definitions: %orted0rra, and %orted0rra,Tester.
%orted0rra, maintains an arra, of sorted object and %orted0rra,Tester
tests the %orted0rra, class.
"#amine the overloa)e) &nsert metho)s
7. %croll down to the first nsert method in the %orted0rra, class. This
method inserts a new nteger into the correct position in the arra,. The
first loop wal+s the arra, until it reaches an object whose sort order is
greater than the object being inserted. The call
9mFTest%ort"rder.nvo+e9 tests the relative sort order of the new object
to an object alread, in the arra,. mFTest%ort"rder contains a reference
to a callbac+ function which is passed in to the constructor of the
%orted0rra, class.
:. %croll down to the next method in the file. This method is also called
nsert but instead of ta+ing an nteger as a parameter it ta+es an arra,
of ntegers. The two nsert methods overload one another and each is
declared with the 9"verloads9 +e,word to ma+e this explicit.
+tart e#ecution an) brea( within the +orte)rra/'ester class
<. %croll down to the Build%orted0rra, procedure of the %orted0rra,Tester
class and add a brea+point in this procedure b, clic+ing in the left margin
of its declaration statement.
=. 'ress #< and then clic+ on the 3aunch %orted 0rra, button on the
0ctiveTas+s form. Execution will stop at the brea+point.
7. The first line of Build%orted0rra, creates an instance of the %orted0rra,
class. The constructor is passed the size of the arra, to be created and a
reference to the %ort0scending method that is defined directl, after
Build%orted0rra,.
+te! through the constructor of +orte)rra/
>. 'ress #11 twice to step into the constructor for the %orted0rra, class.
8ote that the parameter- which is passed the reference to the
%ort0scending function- is declared to be of t,pe Test%ort"rder. This t,pe
is defined in the delegate declaration that appears immediatel, above the
constructor. The delegate declaration defines the signature of methods
that can be passed to the constructor. "nl, procedures whose parameter
t,pes and return t,pe match those in the delegate declaration can be
passed in.
?. Dsing #1A step through the constructor. The last statement of the
constructor stores the reference to the %ort0scending method in the
mFTest%ort"rder variable.
1A. %tep bac+ into Build%orted0rra,.
+te! through calls to the overloa)e) insert metho)
11. %tep using #11 until the first call to the nsert method is reached. This
will step into the first call to nsert on the %orted0rra, class. 8ote that
this call is passing in a single integer.
16. 'ress #11 to step into the call to the nsert method that ta+es a single
nteger parameter.
7
VB Lab: VB Language
17. 'ress %hift$#11 to step bac+ to the caller to the second call to nsert. This
second call is passed an arra, of integers.
1:. 'ress #11 to step into the call to the nsert method that ta+es an arra, of
integer. This method iterates over the arra, of integers passing each one
to the overloaded nsert method that ta+es a single integer.
+te! through the insert metho) an) into the callbac( function
1<. (ontinue pressing #11 to step into the nsert method that ta+es a single
integer.
1=. (ontinue pressing #11 and ,ou will step into the while loop which finds
the correct position for inserting the new integer into the sorted arra,.
17. Dsing #11- step into the mFTest%ort"rder.nvo+e method. 8ote that this
calls into the %ort0scending method of the %orted0rra,Tester class.
%ort0scending compares the two integers and returns the result in the
third parameter.
Finish e#ecution
1>. 'ress #< to continue execution and switch to the Tas+s form.
8

You might also like