Title: Distance Learning Center
1Distance Learning Center
- Lecture 13
- Introduction to Visual Basic
- Programming
- Melissa Lin, IT Consultant
- HenEm, Inc. Parkville, Missouri
- linm_at_ipfw.edu
- http//www.etcs.ipfw.edu/linm
2Lecture 13
- Review
- Classes, objects, properties, and methods
- OOP terminology
- Constructor
- Destructor
- Overloading
- Inheritance
- Overriding
- Object Browser
- Problem 6.7
3Review - Class
- Creating a new object based on a class
- Create an instance of the class by using the New
keyword - General Form
New className( )
Dim arialFont As Font New Font ("Arial",
12) messageLabel.Font arialFont OR messageLabel
.Font New Font ("Arial", 12)
4Specifying a Namespace
- In your projects, you have noticed the Inherits
clause when VB creates a new form class
- Entire namespace is not needed for any classes in
the namespaces that are automatically included in
a Windows Forms project which include - System
- System.Windows.Forms
- System.Drawing
- When referring to a class in a different
namespace - Write out the entire namespace or
- Add an imports statement at the top of the code
to specify the namespace
5Design a Class Define Properties
- Analyze characteristics needed by new objects
- Characteristics or properties are defined as
variables - Define the properties as variables in the class
module - Analyze behaviors needed by new objects
- Behaviors are methods
- Define the methods as sub procedures or functions
- Define variables inside the Class module by
declaring them as Private - Do not make Public, that would violate
encapsulation (each object should be in charge of
its own data)
6Property Procedure General Form
Private ClassVariable As DataType Public
Property PropertyName( ) As DataType Get Return
ClassVariable PropertyName ClassVariable End
Get Set (ByVal Value As DataType) statements
, such As validation ClassVariable
Value End Set End Property
7Read-Only Write-Only Properties
- In some instances a value for a property should
only be retrieved by an object and not changed - Create a read-only property by using the ReadOnly
modifier - Write only the Get portion of the property
procedure
Public ReadOnly Property PropertyName( ) As
DataType
- At times a property can be assigned by an object
but not retrieved - Create a property block that contains only a Set
to create a write-only property
Public WriteOnly Property PropertyName( ) As
DataType
8Class Methods
- Create methods by coding public procedures within
a class - Methods declared with the Private keyword are
available only within the class - Methods declared with the Public keyword are
available to external objects
9Constructors and Destructors
- Constructor
- Method that automatically executes when an object
is instantiated - Constructor must be public and is named New
- Destructor
- Method that automatically executes when an
object is destroyed
10Constructor (continued)
- Overloading Constructor
- It means that two methods have the same name but
a different list of arguments (the signature) - It creates by giving the same name to multiple
procedures in your class module, each with a
different argument list - Ex 1. Sub New() Ex 2. Sub New(ByVal
Title As String) - titleString Title
- End End
- Parameterized Constructor
- Constructor that requires arguments
- Allows arguments to be passed when creating an
object - Ex. aBookSale New BookSale(titleTextBox)
Sub New(ByVal Title As String, ByVal Quantity As
Integer, ByVal Price As Decimal) Me.Title
Title Me.Quantity Quantity
Me.Price Price End Sub
11Creating a New Object Using a Class
- It similar to creating a new tool for the toolbox
but not yet creating an instance of the class - The two steps to create a new object
- Declare a variable for the new object
- Instantiate the object using the New keyword
Private aBookSale As BookSale aBookSale New
BookSale( ) Or Dim aBookSale As New Booksale( )
12Creating a New Object Using a Class (continued)
- If object variable is needed in multiple
procedures, declare the object at class level - Instantiate the object
- Only when(if) it is needed
- Inside a Try/Catch block for error handling
(Try/Catch block must be inside a procedure) - Pass values for the arguments at instantiation
when using a parameterized constructor
13Instance Variables versus Shared Variables
- Instance variables or properties
- Separate memory location for each instance of the
object - Shared variables or properties
- Single variable that is available for ALL objects
of a class - Can be accessed without instantiating an object
of the class - Use the Shared keyword to create
Shared Methods can also be created
14Garbage Collection
- Feature of .NET Common Language Runtime (CLR)
that cleans up unused components - Periodically checks for unreferenced objects and
releases all memory and system resources used by
the objects - Microsoft recommends depending on Garbage
Collection rather than Finalize procedures
15Inheritance
- New class can
- Be based on another class (base class)
- Inherit the properties and methods (but not
constructors) of the base class, which can be - One of the VB existing classes
- Your own class
- Cannot inherit constructors from its base class
- Use the Inherits statement following the class
header and prior to any comments
16Overriding Methods
- Methods with the same name and the same argument
list as the base class - Derived class (subclass) will use the new method
rather than the method in the base class - To override a method
- Declare the original method with the Overridable
keyword - Declare the new method with the Overrides keyword
17Overriding
18Creating a Base Class Strictly for Inheritance
- Classes can be created strictly for inheritance
by two or more similar classes and are never
instantiated - For a base class that you intend to inherit from,
include the MustInherit modifier on the class
declaration - In each base class method that must be
overridden, include the MustOverride modifier and
no code in the base class method
19Inheriting Form Classes
- Creating an Inherited Form Class in two ways
- Select Project -gt Add Windows Form and type in a
name for the new Windows form - Select Project -gt Add Inherited Form and type the
name of the new form - Ex. Public Class baseForm
- inherits System.Windows.Forms.Form
- .
- End Class
- Public Class aboutForm
- inherits ProjectName.aboutForm
- .
- End Class
Multiforms
20Managing Multiclass Projects
- VB projects are automatically assigned a
namespace which defaults to the name of the
project - Add an existing class to a project by copying the
file into the project folder and then adding the
file to the project - Project/Add Existing Item
21Form Inheritance Example (pp. 255-258)
- Project/Add Inherited Form
- aboutForm enter this name, and click on open
- Inheritance Picker Window
- baseForm select this form
aboutForm
22Form Inheritance Example (pp. 255-258)
- Solution Explorer
- baseForm.vb
- aboutForm.vb
- Double click aboutForm.vb
- Properties Text About Form
- Method Me.Close()
- Public Class aboutForm
- Inherits Multiforms.baseForm
- Public Overrides Sub okButton_click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles okButton.Click - End Sub
- End Class
23Form Inheritance Example (pp. 255-258)
- Solution Explorer
- baseForm.vb
- aboutForm.vb
- summaryForm
- mainForm
- aboutForm
- Public Class summaryForm
- Inherits System.Windows.Forms.Form
- Public Overridable Sub okButton_Click(ByVal
sender As System.Object, _ - ByVal e As System.EventArgs) Handles
okButton.Click - ' Allow inherited classes to override this
method. - Me.Close()
- End Sub
24Using the Object Browser
- Use it to view the names, properties, methods,
events and constants of VB objects, your own
objects, and objects available from other
applications - To Display
- Click on Tab in Editor Window
- Object Browser toolbar button
25Object Browser
Find Symbol
Browse list
Objects list
Members list
Namespace icons
Method icon
Property icon
Constants icon
Description Pane
Class icon
26Examine Classes - class view
- Shows all classes of your project
- Classes include
- Form Objects
- Objects on Forms
27Hands-On Programming Example (pp. 262-274)
- Create a project with multiple Forms that have a
shared design element, including a main Form, an
About Form, and a Summary Form that displays the
sales summary information - Design a Base Form to use for inheritance and
make the other three forms inherit from the base
form - The About Form and Summary Form must have an OK
button, which closes the form - The main form will have menus without OK button.
- Main Form File (Calculate Sale, Clear, Summary,
Exit) Help (About)
28Planning GUI Design
File
Help
About
Calculate Sale
Clear
Summary
Exit
Title
Quantity
Price
Student
Extended Price
29Define GUI
30Define GUI
- Start up
- the Project
- Base Form Class
- baseForm
31Main Form - GUI and Code
- mainForm- Inheriting from
- Base Form
- Add GUIs, change properties
- Menus
- Text File, Edit, Help
- Complete event procedures (pages 270-271)
32Place Menu on the Form
Create Menu and SubMenu
33Summary Form GUI and Code
- summaryForm
- Inheriting
- From Base From
- Add GUIs, change roperties
- Complete obButton_click event procedure (page
271)
34Output
- Click
- Calculate
- Selection,
- After
- Entering
- all inputs
35Output (continued)
Click About
36Problem 6.7 in Text Book
- Modify Programming Exercise (5.2) (the check
transactions) to separate business logic from the
user interface - Create a Transaction Class and derived classes
for deposit, Check, and Service Charges - Display summary information on a separate form
rather than a message box
Note that the 1st edition of the text mistakenly
has 5.2 here.
37Authors structure for 5.2
38Code StructureAuthors solution of Problem 5.2
- Private balanceDecimal As Decimal
- Private totalChecksDecimal, totalDepositsDecimal,
totalChargesDecimal As Decimal - Private depositsInteger, checksInteger,
chargesInteger As Integer - Private Sub fileTransactionMenuItem_Click
- Private Sub fileSummaryMenuItem_Click
- Private Sub editClearMenuItem_Click
- Private Sub fileExitMenuItem_Click
- Private Sub editFontMenuItem_Click
- Private Sub editColorMenuItem_Click
- Private Sub helpAboutMenuItem_Click
- Private Function Deposit
- Private Function Check
- Private Function ServiceCharge
See Ex0502
39Code for frmChecking
Create instance of the form
Get TotalChecks property
Show the form
See Ex0502
40Structure of 6.7
See Ex0607
Transaction is the BaseClass
41Code for helpAboutMenuItem
See Ex0607
See Ex0502
42Code for fileTransactionMenuItem
See Ex0502
43Code for Transaction Class
See Ex0607
44Overridable Sub in Base Form
- Public Class baseFrom
- Inherits System.Windows.Forms.Form
- Pulic Overridable Sub okButton_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles okButton.Click - 'This button is inherited on other forms
- End Sub
- End Class
- Public Class summaryForm
- Inherits EX0607.baseFrom
- Public Overrides Sub okButton_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) - 'Close the summary form
- Me.Close()
- End Sub
- End Class
Overridable Sub with no code in Base Form!
Provides for this form to inherit all properties
of baseFrom
Overrides Sub in Base Form and closes this Form!
See Ex0607
45Overrides Subs in Code Classes
See Ex0607
46Overrides Subs in Code Classes
See Ex0607
47Summary
- Review - Class
- classes, objects, properties and methods
- OOP terminology
- Constructor
- Destructor
- Overloading
- Inheritance
- Overriding
- Object Browser
- Problem 6.7
48Question?