Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)"— Presentation transcript:

1 VB Classes ISYS 512/812

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

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

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

5 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

6 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

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

8 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

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

10 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

11 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 4 Then hiddenJobCode = 1 Else hiddenJobCode = Value End If End Set Get JobCode = hiddenJobCode End Get End Property End Class

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

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

14 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

15 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

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

17 Constructor Example Public 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.

18 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

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

20 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

21 Code Using Assembly Imports MyClassDemo Public 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

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

23 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

24 Inheritance Example Public 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

25 Inherit Referenced Classes Imports MyClassDemo Public Class TestInherit Inherits DemoCls1 Public test As String End Class Public Class myTextBox Inherits System.Windows.Forms.TextBox Public myProperty As String End Class


Download ppt "VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)"

Similar presentations


Ads by Google