Microsoft Visual Basic 2008: Reloaded Third Edition - PowerPoint PPT Presentation

Loading...

PPT – Microsoft Visual Basic 2008: Reloaded Third Edition PowerPoint presentation | free to view - id: 3e844-MzAyN



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Microsoft Visual Basic 2008: Reloaded Third Edition

Description:

... code for the Gadis Antiques application ... of the Gadis Antiques application's code ... different version of the code for the Gadis Antiques application ... – PowerPoint PPT presentation

Number of Views:483
Avg rating:3.0/5.0
Slides: 37
Provided by: peopleMor
Category:

less

Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Microsoft Visual Basic 2008: Reloaded Third Edition


1
Microsoft Visual Basic 2008 Reloaded Third
Edition
  • Chapter Eight
  • Sub and Function Procedures

2
Objectives
  • After studying this chapter, you should be able
    to
  • Explain the difference between a Sub procedure
    and a Function procedure
  • Create a Sub procedure and a Function procedure
  • Create a procedure that receives information
    passed to it
  • Explain the difference between passing data by
    value and passing data by reference

3
Objectives (continued)
  • Explain the purpose of the sender and e
    parameters
  • Associate a procedure with more than one object
    and event
  • Utilize a timer control
  • Convert an Object variable to a different type
    using the TryCast keyword

4
Procedures
  • Procedure a block of program code that performs
    a specific task
  • Two types of procedures
  • Function procedure returns a value after
    performing its task
  • Sub procedure does not return a value

5
Sub Procedures
  • Two types of Sub procedures event and
    independent Sub procedures
  • Event procedure
  • Sub procedure that is associated with a specific
    object and event
  • Automatically processed when the associated event
    occurs
  • Independent Sub procedure
  • Collection of code that can be invoked from one
    or more places in an application
  • Not associated with an event
  • Processed only when called (invoked)

6
Sub Procedures (continued)
  • Independent Sub procedures are used to
  • Eliminate the duplication of code in different
    parts of a program
  • Allow a large, complex application to be broken
    into small and manageable tasks
  • Allow multiple programmers to work on an
    application simultaneously

7
Figure 8-1 How to create an independent Sub
procedure
8
Sub Procedures (continued)
  • Independent Sub procedures
  • Have a procedure header and procedure footer
  • Use Pascal case for names
  • Optionally contain a parameter list
  • Parameters
  • Declared in the procedure header
  • Store the information passed into the procedure
    when it is invoked
  • Call statement invokes an independent Sub
    procedure

9
Sub Procedures (continued)
Figure 8-2 How to call an independent Sub
procedure
10
The Gadis Antiques Application
Figure 8-3 Sample run of the Gadis Antiques
application
11
Figure 8-4 Partial code for the Gadis Antiques
application
12
Including Parameters in an Independent Sub
Procedure
  • Parameter stores data that is passed to the
    procedure when the procedure is invoked
  • When calling a procedure with parameters, you
    must pass
  • The same number of arguments
  • The same type of arguments
  • The arguments in the same order as declared in
    the procedure
  • Can pass a variable, named constant, literal
    constant, or keyword as parameter

13
Passing Variables
  • Each variable has a value and a unique memory
    address
  • Variable can be passed to a procedure in two
    ways
  • By value you pass the variables value
  • By reference you pass the variables address
  • By value the procedure receives only the value
    and cannot change the actual variables value
  • By reference the procedure receives the address
    and can make changes to the variables value

14
Passing Variables by Value
  • Use the keyword ByVal before the parameter in the
    procedure declaration
  • ByVal is the default method of passing variables
  • Procedure cannot change the actual variables
    value

Figure 8-5 Sample run of the Pet Information
application
15
Figure 8-6 Partial code for the Pet Information
application
16
Passing Variables by Reference
  • Use the keyword ByRef before the parameter in the
    procedure declaration
  • Procedure receives the address of the variable
    and is able to change the variables value

Figure 8-7 Sample run of the Gross Pay
application
17
Figure 8-8 Partial code for the Gross Pay
application
18
Figure 8-9 Contents of memory after the
CalcGrossPay procedure header and selection
structure are processed
Figure 8-10 Contents of memory after the
appropriate variables and variable name are
removed
19
Associating a Procedure with Different Objects
and Events
  • Handles keyword
  • Appears in event procedure header
  • Indicates the object and event associated with
    the procedure
  • Controls when the procedure is invoked
  • By default, the event procedure name matches the
    name of the associated object and event

20
Figure 8-11 Some of the Gadis Antiques
applications code
21
Associating a Procedure with Different Objects
and Events (continued)
  • Event procedure
  • Name of event procedure can be changed
  • Can be associated with more than one object and
    event as long as each event has the same
    parameters
  • Add the additional object/events to the Handles
    clause
  • Sender parameter contains the memory address of
    the object that raised the event
  • e parameter contains additional information
    about the object that raised the event

22
Figure 8-12 A different version of the code for
the Gadis Antiques application
23
Function Procedures
  • Function procedure (or Function)
  • Block of code that performs a specific task
  • Returns a value after completing its task
  • Visual Basic contains many built-in functions
  • You can create your own functions with or without
    parameters
  • A function is invoked by including its name with
    any arguments in a statement

24
Function Procedures (continued)
  • Function procedure header
  • As datatype clause indicates the type of the
    return value
  • Function procedure footer statement
  • End Function
  • Return keyword
  • Sets the value to be returned by the function
  • Ends the function

25
Figure 8-13 How to create a Function procedure
26
The Pine Lodge Application
Figure 8-14 Sample run of the Pine Lodge
application
27
Figure 8-15 Partial code for the Pine Lodge
application
28
The Pine Lodge Application (continued)
Figure 8-15 Partial code for the Pine Lodge
application (continued)
29
The Timer Control
  • Timer control processes code at one or more
    regular intervals
  • Interval property specifies the length of time
    (in milliseconds) until the next Tick event
    occurs
  • Tick event occurs after each interval has
    elapsed
  • Timer control does not appear on the form, but in
    the component tray
  • Component tray area in the IDE that stores all
    controls that do not appear in the user interface
    at runtime
  • Enabled property must be True for timer to run

30
Figure 8-16 Timer shown in the Component tray
31
The Timer Control (continued)
Figure 8-17 The carTimers Tick event procedure
32
Programming Tutorial
  • The Concentration Game

Figure 8-19 User interface
33
Programming Example
  • Rainfall application

Figure 8-35 User interface
34
Summary
  • Function procedures return a value Sub
    procedures do not return a value
  • Event procedure a Sub procedure associated with
    one or more objects and events
  • Independent Sub and Function procedures not
    associated with any specific object or event
  • Call statement used to invoke a procedure
  • When calling a procedure, you must pass the same
    number, type, and order of parameter values as
    those declared in the procedure

35
Summary (continued)
  • Values can be passed to a procedure by value or
    by reference
  • By Value
  • Provides only the value of the variable to the
    procedure
  • Use the ByVal keyword
  • By Reference
  • Provides the address of the variable to the
    procedure, allowing the procedure to change the
    variables value
  • Use the ByRef keyword

36
Summary (continued)
  • Variables in the parameter list in a procedure
    header have procedure-level scope
  • Timer control used to process code at one or
    more specified intervals
  • TryCast keyword allows you to convert an object
    from one data type to another
About PowerShow.com