Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes."— Presentation transcript:

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

2 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

3 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

4 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

5 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

6 Sub Procedure (Example) Clear the contents of three text boxes Private Sub ClearTextBoxes() txtPrefix.Text = NullChar txtFirstName.Text = NullChar txtLastName.Text = NullChar End Sub

7 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

8 Calling Sub Procedures The optional Call keyword is used to call a Sub procedure The following statements are equivalent: Call ClearTextBoxes() ClearTextBoxes

9 Figure 6-1: Calling a Sub Procedure

10 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

11 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 Double Public Function Square(arg As Double) As Double Dim LocalResult As Double LocalResult = arg * arg Return LocalResult End Function

12 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

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

14 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

15 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

16 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

17 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

18 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

19 Figure 6-4: The Solution Explorer Containing Multiple Modules

20 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

21 Figure 6-5: Add New Item Dialog Box

22

23 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)

24 Figure 6-6: Project Property Pages

25 Project Properties Enable XP Visual Styles causes buttons to appear using a rounded visual style Make single instance application prohibits more than one copy of the application from running Save My.Settings on Shutdown causes application settings to be saved Splash screen defines the splash screen for an application Assembly Information button displays a dialog box to define assembly information

26 Figure 6-7: Assembly Information Dialog Box

27 Operations on Forms Form instances must be created Forms must be displayed Visible forms must be hidden or closed Form events can be handled

28 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

29 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"

30 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"

31 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"

32 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

33 Displaying Forms (Example) Display a form as a modal dialog box frmAbout.ShowDialog() Display a form as a modeless dialog box frmAbout.Show()

34 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

35 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

36 Figure 6-7: Order of Form Events

37 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

38 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.TextBox Friend WithEvents txtUserID As _ System.Windows.Forms.TextBox

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

40 Figure 6-11: Variable Accessibility

41 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

42 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

43 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)

44 Figure 6-12: UML Class Diagram

45 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 (:)

46 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

47 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

48 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 Function End Class

49 Creating Fields and Properties Fields are created by declaring variables in a class with the Public keyword –Public variables violate the rules of encapsulation Property procedures solve the problem by enforcing encapsulation rules

50 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

51 Property Procedures (Syntax) [ReadOnly | WriteOnly | Property] varName ([parameterList]) [As type] [Get [statements] [End Get] [Set(ByVal value As type) [statements] End Set] End Property

52 Property Procedures (Example) Declare a Property procedure named EmployeeName Private HiddenName As String Public Property EmployeeName() As String Get Return HiddenName End Get Set(ByVal value As String) HiddenName = value End Set End Property

53 Figure 6-13: Executing Property Procedures

54 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

55 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

56 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

57 Constructor (Example) A constructor without arguments Public Class BankAccount Private HiddenDateCreated As Date Public Sub New() HiddenDateCreated = Now End Sub End Class

58 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

59 Figure 6-14: Calling a Constructor

60 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


Download ppt "Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes."

Similar presentations


Ads by Google