1 / 19

VB Classes

VB Classes. ISYS 512. Adding a Class to a Project. Project/Add C lass *** 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.

vondra
Télécharger la présentation

VB Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. VB Classes ISYS 512

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

  3. Anatomy of a Class Module Class Module Exposed Part Hidden Part Public Variables & Property Procedures Private Variables Public Procedures & Functions Private Procedures & Functions • Private variables and procedures can be created for internal use.

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

  5. Using a Class • Define a class variable using New • Example: Dim MyEmp As New Emp

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

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

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

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

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

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

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

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

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

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

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

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

  18. .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. • Class • Object Browser

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

More Related