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

Post on 21-Dec-2015

219 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

TRANSCRIPT

VB Classes

ISYS 573

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.

• 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.

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.

Anatomy of a Class Module

Class Module

Public Variables & Property Procedures

Public Procedures & Functions

Exposed Part

Private Variables

Private Procedures & Functions

Hidden Part

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

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.

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

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.

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.

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?

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

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.

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.

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

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.

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

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.

Shared Variable Example: Number of Times

Instances are Created.

Shared instanceCount As Integer

Public Sub New()

instanceCount += 1

MessageBox.Show(instanceCount.ToString)

End Sub

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

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

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)

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

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

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

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

top related