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

Slides:



Advertisements
Similar presentations
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
Advertisements

Tutorial 12: Enhancing Excel with Visual Basic for Applications
The Web Warrior Guide to Web Design Technologies
Chapter 11: Classes and Objects
Chapter 7: Sub and Function Procedures
Chapter 6 Multiform Projects Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
VBA Modules, Functions, Variables, and Constants
Using Visual Basic 6.0 to Create Web-Based Database Applications
VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Multiple Document Interface (MDI) application
ASP.NET Programming with C# and SQL Server First Edition
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Chapter 8 Using Repetition with Loops and Lists. Class 8: Loops and Lists Write Do loops to execute statements repeatedly Write For loops to execute statements.
Chapter 3 Introduction to Event Handling and Windows Forms Applications.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
Tutorial 7: Sub and Function Procedures1 Tutorial 7 Sub and Function Procedures.
McGraw-Hill© 2007 The McGraw-Hill Companies, Inc. All rights reserved. 1-1.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Microsoft Visual Basic 2005: Reloaded Second Edition
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
Tutorial 11 Using and Writing Visual Basic for Applications Code
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 6 Multiple Forms.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
Multiple Forms, Standard Modules, And Menus
Using Visual Basic 6.0 to Create Web-Based Database Applications
Why to Create a Procedure
Using Arrays and File Handling
Multiple Forms, Container Controls, AddHandler This presentation is based on the Forms and ContainerControls VB Projects 1.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
Lecture Set 1 Part C: Understanding Visual Studio and.NET – Applications, Solutions, Projects (no longer used – embedded in Lecture Set 2A)
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Lecture Set 2 Part A: Creating an Application with Visual Studio – Solutions, Projects, Files.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 6 Multiform Projects.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Chapter 11: Introduction to Classes. In this chapter you will learn about: – Classes – Basic class functions – Adding class functions – A case study involving.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
6-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Lecture Set 2 Part A: Creating an Application with Visual Studio – Solutions, Projects, Files 8/10/ :35 PM.
Tutorial 81 Field, Record, Data File Field - a single item of information about a person, place, or thing Record - a group of related fields that contain.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Creating New Forms Projects can appear more professional when using different windows for different types of information. Select Add Windows Form from.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
IS 350 Course Introduction. Slide 2 Objectives Identify the steps performed in the software development life cycle Describe selected tools used to design.
Chapter 8 Multiple Forms, Modules, and Menus. Introduction This chapter demonstrates how to: – Add multiple forms to a project – Create a module to hold.
Introduction to Object-oriented Programming
IS 350 Application Structure
Chapter 1: An Introduction to Visual Basic 2015
Object-Oriented Programming: Classes and Objects
Using Procedures and Exception Handling
Object-Oriented Programming: Classes and Objects
Creating and Using Classes
Variables and Arithmetic Operations
VISUAL BASIC.
Chapter 6 Multiform Projects
Lecture Set 7 Procedures and Event Handlers
CIS16 Application Development Programming with Visual Basic
Lecture Set 11 Creating and Using Classes
Presentation transcript:

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

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

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

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

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

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

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

Figure 6-1: Calling a Sub Procedure

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

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

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

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

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

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

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

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

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

Figure 6-4: The Solution Explorer Containing Multiple Modules

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

Figure 6-5: Add New Item Dialog Box

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)

Figure 6-6: Project Property Pages

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

Figure 6-7: Assembly Information Dialog Box

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

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

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"

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"

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"

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

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

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

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

Figure 6-7: Order of Form Events

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

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

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

Figure 6-11: Variable Accessibility

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

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

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)

Figure 6-12: UML Class Diagram

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

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

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

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

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

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

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

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

Figure 6-13: Executing Property Procedures

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

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

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

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

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

Figure 6-14: Calling a Constructor

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