Programming with Microsoft Visual Basic 2008 Fourth Edition - PowerPoint PPT Presentation

Loading...

PPT – Programming with Microsoft Visual Basic 2008 Fourth Edition PowerPoint presentation | free to download - id: 4a4b4a-NTA4N



Loading


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation
Title:

Programming with Microsoft Visual Basic 2008 Fourth Edition

Description:

Programming with Microsoft Visual Basic 2008 Fourth Edition Chapter Seven Sub and Function Procedures ... – PowerPoint PPT presentation

Number of Views:382
Avg rating:3.0/5.0
Slides: 49
Provided by: facultyTa4
Category:

less

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

Title: Programming with Microsoft Visual Basic 2008 Fourth Edition


1
Programming with Microsoft Visual Basic 2008
Fourth Edition
  • Chapter Seven
  • Sub and Function Procedures

2
Previewing the Harvey Industries Application
  • Open the Harvey Industries.exe file
  • The Harvey Industries application calculates
    payroll for an employee

3
Previewing the Harvey Industries Application
(continued)
Figure 7-1 Payroll calculations shown in the
interface
4
Lesson A Objectives
  • After studying Lesson A, you should be able to
  • Explain the difference between 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
  • Create a Function procedure

5
Procedures
  • Procedure
  • Block of program code that performs specific task
  • Two types of procedures in Visual Basic
  • Sub procedure Does not return value
  • Function procedure Does return value

6
Sub Procedures
  • Two types of Sub procedures in Visual Basic
  • Event procedures
  • Independent Sub procedures
  • Event procedure Associated with specific object
    and event
  • Processed automatically in response to event
  • Independent Sub procedure Independent of any
    object and event
  • Invoked from code using Call statement
  • Parameter Data passed to procedure when invoked

7
Sub Procedures (continued)
  • Call statement Used to invoke procedure
  • Syntax Call procedurename(argumentlist)
  • argumentlist Used to pass information (optional)
  • Argument Data item in argumentlist
  • Parameter Data item in parameterlist
  • Relationship between arguments and parameters
  • Should agree in number, position, and data type
  • Types of data that can be passed as arguments to
    a procedure
  • Variable, literal constant, named constant,
    keyword

8
Sub Procedures (continued)
Figure 7-2 Syntax of an independent Sub
procedure and the Call statement
9
Passing Variables
  • A variable has value and unique address in memory
  • You can pass either variables value or its
    address to procedure
  • Passing by value
  • Passes copy of value stored in variable
  • Passing by reference
  • Passes memory address of variable
  • Allows procedure to change contents of variable

10
Passing a Variable by Value
  • Passing by value Provides only contents of
    variable to receiving procedure
  • How to pass by value
  • Include keyword ByVal before parameter
  • Reasons to pass by value
  • Procedure needs to know contents of variable
  • Procedure does not need to change original value
  • By default, Visual Basic passes by value

11
Passing a Variable by Value (continued)
Figure 7-4 Additional lines of code entered in
the ShowMsg procedure
12
Passing a Variable by Value (continued)
Figure 7-5 ShowMsg procedure and btnDisplay
Click event procedure
13
Passing a Variable by Reference
  • Passing by reference Provides address (memory
    location) of variable to procedure
  • Receiving procedure can thus access variable
  • Reason to pass by reference
  • Procedure needs to change variables contents
  • How to pass by reference
  • Include keyword ByRef before parameter

14
Passing a Variable by Reference (continued)
Figure 7-8 CalcGrossPay procedure and btnCalc
contros Click event procedure
15
Passing a Variable by Reference (continued)
Figure 7-9 Desk-check table before the computer
processes the Call statement
16
Passing a Variable by Reference (continued)
Figure 7-10 Desk-check table after the computer
processes the Call statement and the CalcGrossPay
procedure header
17
Passing a Variable by Reference (continued)
Figure 7-11 Desk-check table after the computer
processes the first statement in the CalcGrossPay
procedure
18
Passing a Variable by Reference (continued)
Figure 7-12 Desk-check table after the computer
processes the statement in the selection
structures true path
19
Passing a Variable by Reference (continued)
Figure 7-13 Desk-check table after the
CalcGrossPay procedure ends
20
Function Procedures
  • Function procedure
  • Block of code that performs specific task
  • Returns value after completing its task
  • Visual Basic provides built-in functions
  • Can also create your own functions
  • As datatype in header indicates return type of
    data
  • Return expression type must agree with As datatype

21
Function Procedures (continued)
Figure 7-14 Syntax, example, and steps for
creating a function
22
Function Procedures (continued)
Figure 7-15 Examples of invoking the GetNewPrice
function
23
Function Procedures (continued)
Figure 7-16 CalcGrossPay function and btnCalc
controls Click event procedure
24
Lesson A Summary
  • Two types of procedures Event and independent
  • Function Performs task and returns value
  • Independent procedures and functions are called
    from applications code using Call statement
  • Pass by value Send copy of variables contents
    to procedure or function
  • Pass by reference Send variables address to
    procedure or function

25
Lesson B Objectives
  • After studying Lesson B, you should be able to
  • Include a combo box in an interface
  • Add items to a combo box
  • Select a combo box item from code
  • Determine the item either selected or entered in
    a combo box
  • Code a combo boxs TextChanged event procedure

26
Including a Combo Box in an Interface
  • Combo box
  • Allows user to select from number of choices
  • Allows user to type entry not on list
  • Can save space on form
  • List box does not share features two and three
  • DropDownStyle property
  • Values Simple, DropDown (default), DropDownList

27
Including a Combo Box in an Interface (continued)
Figure 7-19 Examples of the combo box styles
28
Including a Combo Box in an Interface (continued)
  • Change in item value causes TextChanged event
  • Use Item collections Add method to add item
  • Other properties of combo box
  • Sorted Sorts items in dictionary order
  • SelectedIndex Used to select item in list
    portion
  • SelectedItem Determines which item is selected
  • Text Used to get or set value in text portion

29
Including a Combo Box in an Interface (continued)
Figure 7-20 Code used to add items to the combo
boxes and also select a default item
30
Including a Combo Box in an Interface (continued)
Figure 7-22 Interface showing the gross pay
31
Lesson B Summary
  • Combo box displays list of items for selection
  • Combo box allows user to type entry not on list
  • Specify style of combo box using DropDownStyle
    property
  • Use Items collections Add method to add items to
    Combo box
  • Use combo boxs Sorted property to sort items in
    combos list

32
Lesson B Summary (continued)
  • Use SelectedIndex, SelectedItem, or Text property
    to select combo box item from code
  • Use SelectedIndex, SelectedItem, or Text property
    to determine item that was selected

33
Lesson C Objectives
  • After studying Lesson C, you should be able to
  • Prevent a form from closing
  • Round a number

34
Coding the Harvey Industries Application
  • Objective
  • Calculate employees weekly gross pay, federal
    withholding tax (FWT), Social Security and
    Medicare (FICA) tax, and net pay
  • Review TOE chart for requirements

35
Coding the Harvey Industries Application
(continued)
Figure 7-25 User interface for the Harvey
Industries application
36
Coding the FormClosing Event Procedure
  • FormClosing event Occurs when form is about to
    be closed because
  • Computer processes Me.Close() statement
  • User clicks Close button on forms title bar
  • Requirement for FormClosing event procedure
  • Verifying that user wants to close application
  • Taking appropriate action based on users
    response
  • To prevent closing, set Cancel property of
    FormClosing procedures e parameter to true

37
Coding the FormClosing Event Procedure (continued)
Figure 26 Pseudocode for the FormClosing event
procedure
38
Coding the FormClosing Event Procedure (continued)
Figure 27 Message box displayed by the
FormClosing event procedure
39
Coding the FormClosing Event Procedure (continued)
Figure 28 Pseudocode for the btnCalc controls
Click event procedure
40
Coding the btnCalc Controls Click Event Procedure
Figure 29 Selection structure entered in the
procedure
41
Coding the GetFwt Function
  • How to calculate weekly taxable wages
  • Multiply number of withholding allowances by
    67.31
  • Subtract this result from weekly gross pay
  • Determining federal withholding tax (FWT)
  • Evaluate weekly taxable wages and filing status
  • Use data to look up FWT in special FWT tables
  • GetFwt function emulates FWT table lookup

42
Coding the GetFwt Function (continued)
Figure 7-31 FWT calculations for a married
taxpayer with taxable wages of 288.46
43
Coding the GetFwt Function (continued)
Figure 7-32 FWT calculations for a single
taxpayer with taxable wages of 600.00
44
Coding the GetFwt Function (continued)
Figure 7-33 Pseudocode for the GetFwt function
45
Coding the GetFwt Function (continued)
Figure 7-34 GetFWT function header and footer
46
Completing the btnCalc Controls Click Event
Procedure
  • Must call GetFwt function from btnCalcs Click
    event procedure
  • Math.Round function Used to round value to
    specific number of decimal places
  • Syntax Math.Round (value, digits)
  • value Numeric value to work on
  • digits Number of places to right of decimal point

47
Completing the btnCalc Controls Click Event
Procedure (continued)
Figure 7-35 Payroll calculations displayed in
the interface
48
Lesson C Summary
  • Use forms FormClosing event procedure to process
    code when form is about to be closed
  • Set Cancel property of FormClosing event
    procedures e parameter to true to prevent form
    from being closed
  • Use Math.Round function to round number to
    specific number of decimal places
About PowerShow.com