chapter 6 understanding the structure of an application: procedures, modules, and classes

56
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Upload: lily-lamb

Post on 28-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Chapter 6

Understanding the Structure of an Application: Procedures, Modules,

and Classes

Page 2: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Class 6: Procedures, Modules, and Classes

• Use multiple forms and classes to build an application

• Describe the purpose of procedures

• Understand applications with multiple modules

• Communicate data between forms

• Create and use Property procedures

Page 3: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Common Application Elements

• Most Windows applications contain elements in a form with event handlers

• These elements include:– Procedures– Splash screens– Other forms– User-defined classes

Page 4: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

The Purpose of Procedures

• Procedures facilitate code reuse because they can be called by event handlers and other procedures

• The classes supplied by the .NET Framework class library are just procedures in a class or other type– WriteLine is a procedure in the System.Console

class, for example

• Procedures are of two types:– Function procedures– Sub procedures

Page 5: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Introduction to Sub Procedures

• Both Sub procedures and Function procedures accept arguments

• Sub procedures do not return a value• The Sub keyword replaces the Function

keyword• Sub procedures are used to perform a generic

task that does not return a value• Sub procedures accept 0, 1, or many

arguments

Page 6: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Sub Procedure (Example)

• Clear the contents of three text boxes

Private Sub ClearTextBoxes()

txtPrefix.Text = NullChar

txtFirstName.Text = NullChar

txtLastName.Text = NullChar

End Sub

Page 7: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Sub Procedure (Example, continued)

• Format a text box passed as an argument

Private Sub FormatTextBox( _

ByVal txtArg As TextBox)

txtArg.ForeColor = Color.LightBlue

txtArg.BackColor = Color.DarkBlue

End Sub

Page 8: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Calling Sub Procedures

• The optional Call keyword is used to call a Sub procedure

• The following statements are equivalent:

Call ClearTextBoxes()

ClearTextBoxes

Page 9: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Figure 6-1:Calling a Sub Procedure

Page 10: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Introduction to Function Procedures

• A Function procedure accepts 0, 1, or many arguments– Arguments are passed to a procedure by reference or by value

• A Function procedure returns a value having a data type– The Return statement returns a value from a Function

procedure– Function procedures work like methods that return a value

• The Exit Function statement causes the function to exit immediately

• The Public, Private, and Friend keywords define a procedure's scope

• Procedures appear in a Class or Module block

Page 11: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Function Procedure (Example)

• A Function procedure named Square that calculates the square of its argument– The procedure accepts one argument named arg

• The data type of the argument is Double

– The procedure returns a value having a data type of DoublePublic Function Square(arg As Double) As Double Dim LocalResult As Double LocalResult = arg * arg Return LocalResultEnd Function

Page 12: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Executing a Procedure

• Call the Function procedure named Square• Store the return value in the variable named

Result

Private Sub btnDemo_Click(. . .) _ Handles btnDemo.Click

Dim Result As Double

Result = Square(34.55)

End Sub

Page 13: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Figure 6-2: Execution Flow of a Function Procedure Call

Page 14: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Common Procedure Errors

• Procedures must be called with the correct number of arguments having the correct data types

• Standard widening type coercion rules apply– More restrictive types will be implicitly

converted to less restrictive types

Page 15: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Creating Procedures in Modules

• Procedures must appear inside of a Class or Module block

• Procedures cannot be nested– A procedure cannot appear inside of another

procedure

Page 16: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Creating Procedures in Modules (Example)

• Create a procedure named Square in the module named MathDemo

Public Module MathDemo

Public Function Square( _ ByVal arg As Double) As Double

Return arg * arg

End Function

End Module

Page 17: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Procedures and Access Modifiers

• Access modifiers control the visibility of procedures– A Private procedure can only be called from

the module containing the procedure declaration

– Procedures declared with the Public and Friend access modifiers can be called by other modules and classes

Page 18: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Introduction to Applications with Multiple Modules

• Applications can contain multiple forms, Module blocks, and Class blocks

• Each form, Module, and Class appears in the Solution Explorer

• A physical file can contain multiple Module and Class blocks

Page 19: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Figure 6-4: The Solution Explorer Containing Multiple Modules

Page 20: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Adding a Module to an Application

• Click Project, Add New Item to activate the Add New Item dialog box

• Select the item to add from the list of installed templates– Class– Module– Login form– Windows form

• Assign a name to the new item• Note that the templates vary based on the installed

Visual Studio edition

Page 21: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Figure 6-5:Add New Item Dialog Box

Page 22: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Setting the Startup Object

• A Windows Application project can have two possible entry points– A procedure named Main in a Module block– A form in the application– Set the startup object using the Project property

pages (Application tab)

Page 23: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Figure 6-6:Project Property Pages

Page 24: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Operations on Forms

• Form instances must be created

• Forms must be displayed

• Visible forms must be hidden or closed

• Form events can be handled

Page 25: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Creating and Displaying Multiple Form Instances

• There are three ways to create and display a form– Explicitly create an instance of the form class

and call the Show method– Use the default form instance provided by the My.Forms object

– Use the default form instance name

Page 26: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Creating a Form Instance Explicitly

• Create a form instance the same way as any class instance would be created

• Example to create an instance of the form named frmAbout:Dim frmNewInstance As New frmAbout

frmNewInstance.Text = "About Form"

Page 27: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Displaying a Form using My.Forms

• The My.Forms collection has a property used to reference each form in an assembly

• The name of the property is the same as the form name

• Example:My.Forms.frmAbout.Text = "About Form"

Page 28: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Displaying a Form using the My Object's Default Property

• A default form instance exists having the same name as the class

• Example:frmAbout.Text = "About Form"

Page 29: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Displaying Forms

• Once created, a form must be displayed• A form can be displayed in two ways

– As a modal form• Modal forms must be hidden or closed before the

parent form can be displayed• Display by calling the ShowDialog method

– As a modeless form• The end user can change input focus between

modeless forms at will• Display by calling the Show method

Page 30: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Displaying Forms (Example)

• Display a form as a modal dialog boxfrmAbout.ShowDialog()

• Display a form as a modeless dialog boxfrmAbout.Show()

Page 31: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Hiding and Closing a Form

• A form can be hidden– The objects for a hidden form still exist– Call the Hide method to hide a form

• The Visible property is set to False

• A form can be closed– The objects for a closed form are destroyed– Call the Close method to close a form

Me.Close

Page 32: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Introduction to Form Events

• Events fire as a form gets focus and loses focus– The Activated event fires each time a form gets

focus

– The Deactivate event fires each time a form loses focus

– The Load event fires when a form is loaded into memory

– The FormClosing event fires just before a form is unloaded from memory

– After a form has closed, the FormClosed event fires

Page 33: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Figure 6-7:Order of

Form Events

Page 34: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Communicating Data Between Forms

• Public variables can be shared between forms (modules) and other assemblies

• Friend variables can be shared between forms and modules in the current assembly

• Private variables can only be used by the module containing the declaration

Page 35: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Control Instances and Friend Data

• By default, control instances are declared with the Friend access modifier

• Thus, they can be referenced by other forms in an application

• Example:Friend WithEvents txtUserName As _ System.Windows.Forms.TextBoxFriend WithEvents txtUserID As _ System.Windows.Forms.TextBox

Page 36: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Figure 6-10: Controlling Accessibility with the Friend and Private Access Modifiers

Page 37: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Figure 6-11:Variable Accessibility

Page 38: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Introduction to Custom Classes

• Custom classes can be created in addition to those supported by the .NET Framework class library

• Custom classes can be reused by other applications

• Class elements– A class has a constructor– Classes typically have methods– Classes typically have properties– Classes may have events

Page 39: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Introduction to Class Design

• Encapsulation refers to the coupling of data and the processes that operate on that data– Data should only be modified by procedures

• Classes have an interface and an implementation– The interface consists of the exposed properties,

methods, and events• The interface is used by other developers

– The implementation forms the hidden part of the class• The implementation is hidden from the developers using

the class

• Each class should be designed to mimic a process or related group of processes

Page 40: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Introduction to UML Class Diagrams

• A class can be modeled using a UML class diagram

• A UML class diagram has three compartments divided by horizontal lines– The top compartment contains the class name– The middle compartment contains the attributes

(properties)– The lower compartment contains the operations

(methods)

Page 41: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Figure 6-12:UML Class Diagram

Page 42: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

UML Class Diagram (Analysis)

• Plus and minus signs denote whether the attribute or operation is public (+) or private (-)

• Arguments appear in the declaration– in indicates an input argument

• The data type of a method's return value also appears following a full colon (:)

Page 43: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Implementing Classes• Create methods using Function and Sub

procedures with the Public access modifier• Create properties with Public fields or Property procedures

• Constructors are created using a Sub procedure named New

• Declare hidden members with the Private access modifier

46

Page 44: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Creating Methods

• Classes have a name, attributes, and operations

• Operations are called methods

• Methods are implemented as Public Function or Sub procedures

• Note that Private procedures are part of the implementation and are not considered methods

Page 45: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Creating Methods (Example)

• Create a method named MakeWithdrawal to decrease the account balance

Public Class BankAccount Private HiddenAccountBalance As Double Public Function MakeWithdrawal( _ ByVal amount As Double) As Double HiddenAccountBalance -= amount End FunctionEnd Class

Page 46: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Introduction to Property Procedures

• Public and Friend variables violate encapsulation rules– It's possible to access class data directly– Data should only be modified by a procedure

• Property procedures are used to implement properties

• Property procedures do not violate encapsulation rules

• Property procedures are similar to Function and Sub procedures

Page 47: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Property Procedures (Syntax)

[ReadOnly | WriteOnly | Property] varName ([parameterList]) [As type]

[Get

[statements]

[End Get]

[Set(ByVal value As type)

[statements]

End Set]

End Property

Page 48: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Property Procedures (Example)• Declare a Property procedure named

EmployeeName

Private HiddenName As StringPublic Property EmployeeName() As String Get Return HiddenName End Get Set(ByVal value As String) HiddenName = value End SetEnd Property

Page 49: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Figure 6-13:Executing Property Procedures

Page 50: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Creating Read-only Properties

• Add the ReadOnly keyword to the property declaration

• Omit the Set block– Including the Set block will cause a syntax

error

Page 51: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Creating Write-only Properties

• Add the WriteOnly keyword to the property declaration

• Omit the Get block– Including the Get block will cause a syntax

error

Page 52: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Introduction to Constructors

• Visual basic classes can have an optional constructor– A constructor is a Sub procedure named New

– The constructor is called automatically when a class instance is created

– Constructors can accept 0, 1, or many arguments

Page 53: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Constructor (Example)

• A constructor without arguments

Public Class BankAccount

Private HiddenDateCreated As Date

Public Sub New()

HiddenDateCreated = Now

End Sub

End Class

Page 54: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Constructor (Example, continued)

• A constructor with arguments

Public Class BankAccount

Private HiddenDateCreated As Date Private HiddenAccountNumber As Integer

Public Sub New(accountNum As Integer) HiddenDateCreated = Now HiddenAccountNumber = accountNum End Sub

End Class

Page 55: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

Figure 6-14:Calling a Constructor

Page 56: Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes

The Difference Between Class and Module Blocks

• Modules cannot have a constructor– It's not possible to create an instance of a module

• Module data and procedures are always shared– Exactly one copy of the data exists

• Class data can be instance data or shared data– Shared data is declared with the Shared keyword

• One copy exists no matter the number of class instances

– One copy of instance data exists for each class instance