Upload
paul-gregory
View
224
Download
0
Tags:
Embed Size (px)
Citation preview
VB Classes
ISYS 512/812
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.
• Polymorphism: The concept of using a single name for different behaviors. – 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.
.Net Framework Class Library Structure
• Assembly:– Basic unit of deployment.
– Implemented as Dynamic Link Library, DLL.
– May contain many Namespace
• NameSpace:– Organize a group of related classes and namespaces.
• Class
• Object Browser
Referencing Assemblies and Classes
• Each project automatically references essential assemblies.– Project property/References– Or: Solution Explorer/Show All Files
• Add additional reference:– Project/Add Reference
Adding a Class to a Project
• Project/Add 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
Anatomy of a Class Module
Class Module
Public Variables & Property Procedures
Public Procedures & Functions
Exposed Part
Private Variables
Private Procedures & Functions
Hidden Part
•Private variables and procedures can be created for internal use.
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.
Private pvEid As String Private pvEname As String Private pvSalary As Double Public Property eid() As String Get eid = pvEid End Get Set(ByVal Value As String) pvEid = Value End Set End Property Public Property eName() As String Get eName = pvEname End Get Set(ByVal Value As String) pvEname = Value End Set End Property Public Property Salary() As Double Get Salary = pvSalary End Get Set(ByVal Value As Double) pvSalary = Value End Set End Property
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 String
Public WriteOnly Property Password() As String
Set(ByVal Value As String)
hiddenPassword=Value
End Set
End 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
• 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.
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.
Difference between Assembly and Class
• A class defined in a project is available to that project only.
• Once a class is compiled in an assembly it can be used by any projects.
• To create an assembly:– Start a Class Library project
Steps to Create An Assembly
• Start a Class Library project• Create classes
– You can also use existing classes defined in other projects by Project/Add Existing Item
• Save project• Select Build/Build to compile the code.
– When the class library is compiled successfully, an assembly is created and stored in the project’s Bin/Release folder.
Using the Assembly
• Reference the assembly: Project/Add Reference and use the Browse button to select the assembly.
• Import the assembly.– Global import:
• Project property windows/References
– Local import• Using the Imports statement
Code Using Assembly
Imports MyClassDemoPublic Class Form1 Dim myButton As New Button Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim myCls As New DemoCls1 MessageBox.Show(myCls.myName) End Sub
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.
Employee Super Class with Three SubClasses
All employee subtypes will have emp nbr, name, address, and date-hired
Each employee subtype will also have its own attributes
Inheritance ExamplePublic Class Emp
Public Eid As String
Public Ename As String
Public salary As Double
Public Function tax() As Double
tax = salary * 0.1
End Function
End Class
Public Class secretary
Inherits Emp
Public WordsPerMinute As Integer
End Class
Inherit Referenced Classes
Imports MyClassDemoPublic Class TestInherit Inherits DemoCls1 Public test As StringEnd ClassPublic Class myTextBox Inherits System.Windows.Forms.TextBox Public myProperty As StringEnd Class