Using a Class Module VB Help

Let’s implement the same project, but this time with a Class Module. The Class Module will completely hide the implementation details. It will expose its functionality and the developer will never see or edit its source code.

Save the existing project and then start a new project (it’s the c timer project in this chapter’S folder on the CD). Follow these steps to create a new Class:

1. In the New Project dialog box select ActiveX DLL. Visual Basic will add a . Class Modules folder in the Project Explorer window and a Crass Module under it. The Class Module is named Class! by default. The ActiveX DLL project doesn’t have Forms.
2. Change the Class Module’s Name property to C Timer
The Class Module doesn’t have a visible interface, so the Code window for the new component will be displayed.
3. Enter the lines in Code .15.3 in the Class Module’s Code window.

The Climer aa55′ Code

Dim total Interval As Double
Dim Tl As Double

Public Sub Start Counting()
TI = Time
End Sub
Public Sub Stop Counting()
totaI interval ·-total interval + T1 = .Tl
End Sub
Property Get.Elapsed Time()As Double’
Elapsed Time = total interval,
End Property
Public Sub Reset Timer~)
total interval = 0
End Sub

The code of the Class Module is quire similar to the Module’s code, but it doesn’t   have a public variable. The total interval variable can’t be accessed directly from any procedure outside the Class Module. It can only be read through the Elapsed Tune() procedure. Notice that this is.a special type procedure called Property Get. Every time  L an application attempts to read the value of the total interval variable the Elapsed Time procedure_is invoked Its code reads the value of total interval variable and assigns it to the Tune() procedure. This value is passed. to the calling application we   an application attempts to set a’property value a’similar procedure is
invoked only it’s a Property’ Lei procedure. This Class doesn’t have any properties that can be set, so there.are no Property Let  both Property procedures .after looking at this simple example. The Property and Property Get procedures act like buffers between the Class and the application that uses it. For one thing, the application can’t-set the variable directly

NOTE

The C Timer Class project doesn’t provide a Property let procedure for the total interval property. As a result. this Property is read-only.

The methods of the Class Module are identical to the methods of the Module. Adding a method to a Class is.as simple as declaring a Public function or subroutine. Any procedure that can appear in your code can become a method of a Class if it’s entered in a Class Module and-declared as Public.

NOTE

Some members can be implemented both as methods. and properties, and It isn’t. always easy to say which way to go. Fortunately, this isn’t extremely important. You should try to follow the paradigm of the built-in, or third party, controls. For . example, if Class is an ActiveX control, a member you would like to see in the
Properties window should be implemented as a property (the opposite isn’t necessarily true). Methods should correspond to actions, while-properties are attributes. The naming scheme is also important. A name like Get Elapsed Time suggests a method, while a name like Elapsed Time is closer to a property.

Posted on November 9, 2015 in More Advanced ActiveX Controls

Share the Story

Back to Top