Download presentation
Presentation is loading. Please wait.
1
VB Classes BICS546
2
Adding a Class to a Project Project/Add Class Class will be saved in a.CLS file Steps: –Adding properties Declare Public variables in the General Declaration section Property procedures: Property Let / Property Get functions –Adding methods –Adding events Default events: Class_Initialize(), Class_Terminate Private variables and procedures can be created for internal use.
3
Anatomy of a Class Module Class Module Public Variables & Property Procedures Public Procedures & Functions Exposed Part Private Variables Private Procedures & Functions Hidden Part
4
Class Code Example Option Explicit Public Eid As String Public Ename As String Public salary As Double Public Function tax() As Double tax = salary * 0.1 End Function
5
Creating Property with Property Procedures Implementing a property with a public variable the property value cannot be validated. We can create read-only or write-once properties with property procedures.
6
Property Procedure Code Example (Demo:Class Module & Builder) Public SSN as String Public Ename as String Public DateHired as Date Private hiddenJobCode as Long Public Property Let JobCode(ByVal jobcodeArg as Long) If jobcodeArg 4 Then hiddenJobCode = 1 Else hiddenJobCode = JobCodeArg End IF End Property Public Property Get JobCode() as Long JobCode = hiddenJobCode End Property
7
Implementing a Read Only Property Use only the Property Get procedure. Ex. Create a YearsEmployed property from the DateHired property: Public Property Get YearsEmployed() As Long YearsEmployed = (Date – DateHired)/365 End Property –Note: It is similar to a calculated field in database. How to create a write-once property?
8
Collections Collections are used to store lists of objects. More flexible than array: –No need to declare the number of objects in a collection, no need to ReDim. –Objects can be added, deleted at any position. –Object can be retrieved from a collection by a key. A collection’s name usually end with a “s”.
9
Using Collections Define a collection: –Ex. Private Customers as New Collection Methods: –ADD: Add object to a collection Dim Customer as New clsCustomer Customers.Add(Customer) Add an object with a key: –Customers.Add(Customer, Customer.CID) –Item: Retrieve an object from a collection with a position index (base 1) or with a key. Set Customer = Customers.Item(1) Set Customer = Customer.Item(“C101”) –Count: Return the number of objects in a collection. –Remove: Delete an object with a position index or key.
10
Iterating Through a Collection Dim Customer as clsCustomer Dim Indx as Long For Indx = 1 to Customers.Count set Customer = Customers.Item(Indx) … class operations … Next Indx For Each Customer In Customers class operations Next Customer
11
Implementing a 1:M Relationship With Class Hierarchy Public did As String Public dname As String Public emps As New Collection Public eid As String Public ename As String Public salary As Currency Class Department Class Employee
12
To Add a New Emp Private Dep As New dept Private Sub Form_Load() Dep.did = "mis" Dep.dname = "Management IS" End Sub Private Sub Command1_Click() Dim newemp As New emp With newemp.eid = Text1.ename = Text2.salary = Val(Text3) End With Text1 = vbNullString Text2 = vbNullString Text3 = vbNullString Dep.emps.Add newemp End Sub
13
To Find Total Salary Private Sub Command3_Click() Dim temp As emp Dim totalsal As Double totalsal = 0 For Each temp In Dep.emps totalsal = totalsal + temp.salary Next temp Label5.Visible = True Label5.Caption = "TotalSalary=" & CStr(totalsal) End Sub
14
Deploying Classes as ActiveX Servers New Project/ActiveX DLL Create the classes you need, or open the classes previously created. Set the class’s Instancing property to 5 – multiuse. To compile, File/Make.DLL VB automatically registers the DLL.
15
Using the DLL Option Explicit Dim test As ActiveXEmp.emp Dim test2 As Object Private Sub Form_Load() Set test = New ActiveXEmp.emp Set test2 = CreateObject("ActiveXEmp.emp") test.eid = "hi" test2.eid = "there" Text1.Text = test.eid Text2.Text = test2.eid End Sub
16
Database Access with Classes VB Chapter 7
17
Data-Aware Classes –Classes that serve as data source. Single-Record-Handling Classes –Retrieves a single record from the database and makes it available to your application in the form of an object. –The fields in the record are exposed as the object’s properties. –Any actions performed by the data (updates, calculations, etc.) are exposed as the object’s methods.
18
Data-Aware Classes (VB Chap 7) A data-aware class has a DataMembers collection. Steps to Setting up the data-aware class: –1. Open a new Standard EXE project. –2. Project/Add Class –3. Use the Properties window to change the class’s DataSourceBehavior to 1 – vbDataSource –4. Project/Reference/Microsoft Activex Data Object –5. In the declaration section of the class, create a private Recordset object.
19
–6. In the class initialize event, write code to create the Recordset object. –7. Write code to register Data Member names to DataMembers collection. Example: DataMembers.Add “DataMemberName” –8. Use the GetDataMember event to return a recordset object based on DataMember name. –9. Write methods to control recordset. Note: Some steps can be automated by:Project/Add Class Module /Data Source
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.