Date post: | 03-Jan-2016 |
Category: |
Documents |
Upload: | erika-randall |
View: | 229 times |
Download: | 6 times |
Chapter 9: Writing Procedures
Visual Basic .NET Programming:
From Problem Analysis to Program Design
Visual Basic .NET Programming: From Problem Analysis to Program Design 2
Objectives
• Explore the client-server model
• Review the syntax of procedures
• Write user-defined procedures
• Design and write overloaded procedures
Visual Basic .NET Programming: From Problem Analysis to Program Design 3
Exploring the Client-Server Model
• Methods
– Procedures within class definition
– Provide services to other objects in system
• Objects send messages to invoke procedures
– Client object sends message
– Server object receives message
Visual Basic .NET Programming: From Problem Analysis to Program Design 4
Visual Basic .NET Programming: From Problem Analysis to Program Design 5
Exploring the Client-Server Model (continued)
• Client sends message invoking server procedure
– Optionally sending values in form of arguments
• Server procedure performs requested task
– Optionally returns value to client
Visual Basic .NET Programming: From Problem Analysis to Program Design 6
Exploring the Client-Server Model (continued)
• Calling method example:
– Console.WriteLine(“Hello World Wide Web”)
• Class: Console
• Method: WriteLine
• Argument: “Hello World Wide Web”
Visual Basic .NET Programming: From Problem Analysis to Program Design 7
Exploring the Client-Server Model (continued)
• Calling method example:
– doubleResult = Math.Sqrt(firstInt)
• Class: Math
• Method: Sqrt
• Argument: firstInt
• Return value: doubleResult
Visual Basic .NET Programming: From Problem Analysis to Program Design 8
Exploring the Client-Server Model (continued)
• Unified Modeling Language (UML) sequence diagram– Maps interactions between objects
– Shows objects as rectangles
– Lifelines• Vertical lines below objects
• Represent sequence of time
• Shown as either dashed line or narrow box
Visual Basic .NET Programming: From Problem Analysis to Program Design 9
Visual Basic .NET Programming: From Problem Analysis to Program Design 10
Exploring the Client-Server Model (continued)
• Unified Modeling Language (UML) sequence diagram (continued)
– Active object
• Object executing or controlling part of interaction
– Messages
• Represented using arrows
Visual Basic .NET Programming: From Problem Analysis to Program Design 11
Example 9-1: Invoking Math.Pow
1. Option Explicit On
2. Option Strict On
3. Module Chapter9Example1
4. Sub Main()
5. Dim answer As Double
6. answer = Math.Pow(4, 2)
7. Console.WriteLine(answer)
8. End Sub
9. End Module
Visual Basic .NET Programming: From Problem Analysis to Program Design 12
Visual Basic .NET Programming: From Problem Analysis to Program Design 13
Reviewing the Syntax of Procedures
• Using procedures
– Improves code clarity
– Enhances error detection
– Simplifies maintenance
• Types of procedure in VB .NET:
– Sub
– Function
Visual Basic .NET Programming: From Problem Analysis to Program Design 14
Using Sub Procedures
• Sub procedure
– Begins with procedure header
– Followed by one or more statements
– Ends with keywords End Sub
Visual Basic .NET Programming: From Problem Analysis to Program Design 15
Visual Basic .NET Programming: From Problem Analysis to Program Design 16
Using Sub Procedures (continued)
• Procedure header parts:
– Accessibility
– Sub
– Procedure name
– Parameter list
Visual Basic .NET Programming: From Problem Analysis to Program Design 17
Using Sub Procedures (continued)
• Parameters
– Values passed to procedure
– Declared using keywords
• ByVal
• ByRef
Visual Basic .NET Programming: From Problem Analysis to Program Design 18
Using Sub Procedures (continued)
• By value – Procedure does not have direct access to argument
variable • Cannot change its contents
– Procedure is given copy of variable
• By reference– Procedure code given direct access to argument
variable • May change its value
Visual Basic .NET Programming: From Problem Analysis to Program Design 19
Using Function Procedures
• Function procedure definition
– Begins with procedure header
– Followed by one or more statements
– Ends with keywords End Function
Visual Basic .NET Programming: From Problem Analysis to Program Design 20
Visual Basic .NET Programming: From Problem Analysis to Program Design 21
Using Function Procedures (continued)
• Procedure header:
– Accessibility
– Function
– Procedure name
– Parameter list
– As dataType
Visual Basic .NET Programming: From Problem Analysis to Program Design 22
Example 9-3: Writing and invoking a Function procedure
1. Option Explicit On
2. Option Strict On
3. Module Client
4. Sub Main()
5. Dim total As Integer
6. total = Server.AddEmUp(2, 3)
7. Console.WriteLine(total)
8. End Sub
9. End Module
Visual Basic .NET Programming: From Problem Analysis to Program Design 23
Example 9-3: Writing and invoking a Function procedure
(continued)3. Module Server
4. Public Function AddEmUp(ByVal a As Integer, ByVal b As Integer) As Integer
5. Dim sum As Integer
6. sum = a + b
7. Return sum
8. End Sub
9. End Module
Visual Basic .NET Programming: From Problem Analysis to Program Design 24
Example 9-3: Writing and invoking a Function procedure
(continued)
• Returning a variable
– Use keyword Return
– Followed by variable
– Returns control of execution to invoking client
Visual Basic .NET Programming: From Problem Analysis to Program Design 25
Visual Basic .NET Programming: From Problem Analysis to Program Design 26
Writing User-Defined Procedures
• User-defined procedures
– Procedures that you design and write
– Contrast to methods contained in FCL classes
– Use to give code structure and organization
– Can be Private or Public
Visual Basic .NET Programming: From Problem Analysis to Program Design 27
Writing User-Defined Procedures (continued)
• Private procedures
– Provide services only to module in which they are defined
– Cannot be invoked by code in other objects
• Public procedures
– Designed specifically to be invoked by other objects
Visual Basic .NET Programming: From Problem Analysis to Program Design 28
Writing Private Procedures
• From Example 9-4:13. Private Sub InputFahrenheit()
14. Console.WriteLine("Please enter a Fahrenheit temperature:")
15. fahrenheit =
Convert.ToDouble(Console.ReadLine())
16. End Sub
Visual Basic .NET Programming: From Problem Analysis to Program Design 29
Writing Private Procedures (continued)
• Scope:
– Determined by where variable is declared
– Controls where variable can be accessed
• Module scope
– Variable declared within module
– Can be accessed by code in module
Visual Basic .NET Programming: From Problem Analysis to Program Design 30
Writing Private Procedures (continued)
• Procedure scope
– Variable declared within procedure
– Only code within procedure can access variable
• Block scope
– Declared within If, Do, or For statement
– Can only be accessed within block
Visual Basic .NET Programming: From Problem Analysis to Program Design 31
Writing Private Procedures (continued)
• Use variables with smallest or narrowest scope possible– Minimizes impact that changes in one procedure
may have in others
• Hide internal workings of procedure from outside code
• Public procedures can be invoked by any object• Design public procedures to provide services that
are required by multiple objects or applications
Visual Basic .NET Programming: From Problem Analysis to Program Design 32
Using Optional Parameters
• May sometimes want to make some arguments optional
• Use keyword Optional
– In parameter declaration
– Provide default value
– Must be placed at end of parameter list
Visual Basic .NET Programming: From Problem Analysis to Program Design 33
Example 9-7: Using an Optional Parameter
• Excerpt:Public Function ComputeShippingCharge(Optional
ByVal distance As Integer = 100) As Double
Visual Basic .NET Programming: From Problem Analysis to Program Design 34
Designing and Writing Overloaded Procedures
• Procedure’s signature consists of
– Name
– Parameter list
• VB .NET identifies procedures by signature
– Can have two or more procedures with same name
– As long as parameter lists are different
Visual Basic .NET Programming: From Problem Analysis to Program Design 35
Designing and Writing Overloaded Procedures
(continued)• Overloading a procedure
– Write multiple procedures with same name but with different signatures
– Convenient technique to avoid using unique names for procedures
– Use Overloads keyword
• Return data type is not part of procedure’s signature
Visual Basic .NET Programming: From Problem Analysis to Program Design 36
Example 9-8: Overloading a procedure (Excepts)
Public Overloads Function ComputeSalesTax(ByVal amount As Single) As Double
Public Overloads Function ComputeSalesTax(ByVal amount As Double) As Double
Visual Basic .NET Programming: From Problem Analysis to Program Design 37
Programming Example: Electricity Billing
• Input
– Previous and current meter reading
• Output
– Electric bill containing:
1. Kilowatt hours (kwh) consumed
2. Electric charge formatted as currency
3. Sales tax formatted as currency
4. Bill total formatted as currency
Visual Basic .NET Programming: From Problem Analysis to Program Design 38
Programming Example: Electricity Billing (continued)
• Problem Analysis and Algorithm Design
– Purpose of program:
• Input previous and current electric meter readings
• Compute and display total bill
– Using sentinel-controlled loop
Visual Basic .NET Programming: From Problem Analysis to Program Design 39
Programming Example: Electricity Billing (continued)
• Procedures used:
– Main
– GetPreviousReading
– GetCurrentReading
– ComputeElectricityCharge:
– DisplayBill
Visual Basic .NET Programming: From Problem Analysis to Program Design 40
Summary
• Two types of procedures in Visual Basic .NET:
– Sub procedures
– Function procedures
• Arguments are passed into parameters
• Two categories of user-defined procedures:
– Public
– Private
Visual Basic .NET Programming: From Problem Analysis to Program Design 41
Summary (continued)
• Scope of variable
– Represents visibility or accessibility
– Determined by where you declare it
• Create optional argument by adding keyword Optional
• Procedure’s signature consists
– Name
– Parameter list
Visual Basic .NET Programming: From Problem Analysis to Program Design 42
Summary (continued)
• Overloading procedure:
– Write multiple procedures with same name but different signatures