Creating a Custom Object

Class modules are useful for trapping events, but they are also valuable because they can be used to create custom objects. When you are creating a custom object, the class module becomes a template of the object's properties and methods. To better understand this, we are going to create an employee object to track employee name, ID, hourly wage rate, and hours worked.

Insert a class module and rename it to clsEmployee. The clsEmployee object has four properties:

■ EmpName—Employee name

■ EmpRate—Hourly wage rate

■ EmpWeeklyHrs—Hours worked

Properties are variables that can be declared Private or Public. These properties need to be accessible to the standard module so they will be declared Public. Place the following lines at the top of the class module:

Public EmpName As String Public EmpID As String Public EmpRate As Double Public EmpWeeklyHrs As Double

Methods are actions that the object can take. In the class module, these actions take shape as procedures and functions. The following code creates a method, EmpWeeklyPay(), for the object that calculates weekly pay:

Public Function EmpWeeklyPay() As Double EmpWeeklyPay = EmpRate * EmpWeeklyHrs End Function

0 0

Post a comment