vb classes isys 573. object-oriented concepts abstraction: –to create a model of an object, for...

27
VB Classes ISYS 573

Post on 21-Dec-2015

217 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

VB Classes

ISYS 573

Page 2: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Object-Oriented Concepts• Abstraction:

– To create a model of an object, for the purpose of determining the characteristics (properties) and behaviors (methods) of the object. For example, a Customer object is an abstract representation of a real customer.

• Encapsulation: – The combination of characteristics of an object along

with its behavior. – Data hiding: Each object keeps its data and procedures

hidden and exposes only those data elements and procedures that it wishes to allow outside world to see.

– The implementation of a class – in other words, what goes on inside the class – is separate from the class’s interface.

Page 3: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

• Inheritance: – The process in which a new class can be based on an existing

class, and will inherit that class’s interface and behaviors. The original class is known as the base class, super class, or parent class. The inherited class is called a subclass, a derived class, or a child class.

– Inherited classes should always have an “is a” relationship with the base class.

– Reusability

• Polymorphism: The concept of using a single name for different behaviors. – Overriding: A derived class with a method named the same as a

method in the base class is said to override the method in the base class. Overriding allows an inherited class to take different actions from the identically named method in the base class.

– Overloading: A class may have more than one methods with the same name but a different argument list (with a different number of parameters or with parameters of different data type), different parameter signature.

Page 4: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Adding a Class to a Project

• Project/Add Window Form/Class– *** MyClass is a VB keyword.

• Steps:– Adding properties

• Declare Public variables in the General Declaration section• Property procedures: Set / Get

– Adding methods– Adding events, exceptions

• Private variables and procedures can be created for internal use.

Page 5: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Anatomy of a Class Module

Class Module

Public Variables & Property Procedures

Public Procedures & Functions

Exposed Part

Private Variables

Private Procedures & Functions

Hidden Part

Page 6: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Class Code Example

Public Eid As String

Public Ename As String

Public salary As Double

Public Function tax() As Double

tax = salary * 0.1

End Function

Page 7: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Creating Property with Property Procedures

• Implementing a property with a public variable the property value cannot be validated by the class.

• We can create read-only, write-only, or write-once properties with property procedure.

• Steps:– Declaring a private class variable to hold the property

value.

– Writing a property procedure to provide the interface to the property value.

Page 8: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Property Procedure Code Example

Public Class Emp2 Public SSN As String Public Ename As String Public DateHired As Date Private hiddenJobCode As Long Public Property JobCode() Set(ByVal Value) If Value < 1 Or Value > 4 Then hiddenJobCode = 1 Else hiddenJobCode = Value End If End Set Get JobCode = hiddenJobCode End Get End Property

End Class

Page 9: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

How the Property Procedure Works?

• When the program sets the property, the property procedure is called and the code between the Set and End Set statements is executed. The value assigned to the property is passed in the Value argument and is assigned to the hidden private variable.

• When the program reads the property, the property procedure is called and the code between the Get and End Get statements is executed.

Page 10: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Implementing a Read-Only Property

• Declare the property procedure as ReadOnly with only the Get block.

• Ex. Create a YearsEmployed property from the DateHired property:

Public ReadOnly Property YearsEmployed() As Long Get YearsEmployed = Now.Year - DateHired.Year End Get End Property

– Note: It is similar to a calculated field in database.

Page 11: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Implementing a Write-Only Property

• Declare the property procedure as WriteOnly with only the Set block.

• Ex. Create a PassWord property:Private hiddenPassword as StringPublic WriteOnly Property Password() As String Set(ByVal Value As String) hiddenPassword=Value End Set End Property

• How to create a write-once property?

Page 12: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Method Overloading Using the Overloads Keyword

Public Overloads Function tax() As Double

tax = salary * 0.1

End Function

Public Overloads Function tax(ByVal sal As Double) As Double

tax = sal * 0.1

End Function

Page 13: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Constructors and Destructors

• A constructor is a method that runs when a new instance of the class is created. In VB .Net the constructor method is always named Sub New.

• A destructor is a method that automatically executes when an object is destroyed.

Page 14: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Constructor ExamplePublic Sub New()

Me.eid = ""

ename = ""

salary = 0.0

End Sub

Public Sub New(ByVal empId As String, ByVal empName As String, ByVal empSal As Double)

eid = empId

ename = empName

salary = empSal

End Sub

Note: Cannot use Overloads with the New.

Page 15: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Constructors and Read-Only Field

• A constructor procedure is the only place from inside a class where you can assign a value to read-only fields.– Public ReadOnly currentDate As Date

– Public Sub New()

– Me.eid = ""

– ename = ""

– salary = 0.0

– currentDate = Today

– End Sub

Page 16: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Shared Methods

• A shared method is available from a class without the need to create an instance of the class.

• To create a shared method, include the Shared keyword in the method definition.

Page 17: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Shared Method Example

Public Class myMath

Public Shared Function sum(ByVal x As Double, ByVal y As Double) As Double

sum = x + y

End Function

Public Shared Function difference(ByVal x As Double, ByVal y As Double) As Double

difference = x - y

End Function

End Class

Page 18: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Shared Variables

• A shared variable is one that is shared by all instances of a class.

• Define the variable with the Shared keyword.– Ex. Shared instanceCount as Integer

• The default scope for shared variables is private, but they can be declared as Public.

Page 19: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Shared Variable Example: Number of Times

Instances are Created.

Shared instanceCount As Integer

Public Sub New()

instanceCount += 1

MessageBox.Show(instanceCount.ToString)

End Sub

Page 20: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Creating a Read-Only ID Property for each Instance of a Class

Public ReadOnly instanceID As Integer

Shared instanceCount As Integer

Public Sub New()

instanceCount += 1

instanceID = instanceCount

End Sub

Page 21: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Same ID Property Using Property Procedure

Shared instanceCount As Integer

Public Sub New()

instanceCount += 1

End Sub

Public ReadOnly Property instanceID() As Integer

Get

instanceID = instanceCount

End Get

End Property

Page 22: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Class Events• Events that class can raise. Different from user

events such as mouse clicks, these events are triggered in code in the class and can be detected by the host program.

• An event is declared in a class definition using the Event keyword. The event declaration statement declares the name of the event and types of arguments that the event has.– Public Event EventName(Argument list)

• To raise an event, use the RaiseEvent statement:– RaiseEvent EventName(Argument List)

Page 23: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Trapping Events with WithEvents

• To trap class events, declare a class variable with the WithEvents keyword.– Dim WithEvents emp1 As New emp()

• Create a event procedure to response to the event:– Every event procedure has a Handles keyword

to identify the event it handles. The name of a event procedure is meaningless.

• Private Sub emp1_calTax() Handles emp1.CalTax

Page 24: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Class Events ExamplePublic Class emp Event calTax() Event readID() Event InvalidJobCode()

Public Function tax(ByVal salary As Double) As Double tax = salary * 0.1 RaiseEvent calTax() End Function

Public ReadOnly Property instanceID() As Integer Get instanceID = instanceCount RaiseEvent readID() End Get End Property

Page 25: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Public Property JobCode()

Set(ByVal Value)

If Value < 1 Or Value > 4 Then

RaiseEvent InvalidJobCode()

Else

hiddenJobCode = Value

End If

End Set

Get

JobCode = hiddenJobCode

End Get

End Property

Page 26: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)

Dim WithEvents emp1 As New emp()

Private Sub emp1_calTax() Handles emp1.calTax MessageBox.Show("tax calculated") End Sub Private Sub emp1_readID() Handles emp1.readID MessageBox.Show("read id") End Sub Private Sub invCode() Handles emp1.InvalidJobCode MessageBox.Show("invalide code") End Sub

Page 27: VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)