Private and Friend Members VB Help

Class Modules may contain private procedures’ too. These procedures can be called from within the Class’ code, but other applications that use the Class can’t call them. If you want to keep track of how many times a method-has been called during  the course of an application, you can include a function like the following one:

Private Function Call Counter() As Integer
Static Popularity As Integer
Popularity = Popularity = 1
Call Count = Popularity
End Function

This function can be called from within the Class’ code, but applications that use this Class can’t see the Call Counter function. Because Call Counter O is private it’s not a method of the Class, it’s a regular function in the Class’ code. You can add this function to the C Timer project and see that you can’t access it from
within another application.

In addition to private and public members, Classes have a third type of scope modifier, which is called Friend A Friend member is public for the entire project and can be accessed by all the Classes in the project. Public variables in a Standard EXE project can be accessed by all other Modules and Forms . In a standard EXE application, this is the broadest scope a variable can have. With Active components
whose public members also an be accessed from other applications there has to be another type that makes members accessible from anywhere in the project but not from outside Keep in mind that only methods (functions or subroutines) and Property properties can be declared as Friend. Variables are either private or public. This means that the Class Modules of an ActiveX DLL project can communicate with each other by calling Friend methods and properties .

Testing the C Timer Class

The C timer ActiveX DLL can’t be tested as is. A Class Module exposes its objects to other applications, but it can’t be executed on its own. Normally, you’d compile the Class Module and then start a new project that would reference the newly created Class. To simplify the process of testing ActiveX components Visual Basic
allows you to build project groups. In other words, you can have the ActiveX component and .its test project in the same project group. To add a test project to the C Timer project, follow these steps:

1. Add a new project with the Pile );-Add Project command. Visual Basic will add a new folder to the Explorer window, the Project folder, and it will place a Form under it. Project is a regular Visual Basic project.
2. Change the name of the new project to Test Project and the name of its Form to Test Form
3. Place the controls you see in Figure 15.1 on the Test Form.
4. It order to use the C Timer Class in the test project we must first add a reference to the Class. Open the Project menu and select References. In the References dialog box select the entry Timer Project by’ checking it, as shown in Figure 15.2. The References dialog box displays the name of the project not the name of the Class.

Add the C timer Class to a
project through the References
dialog box.

FIGURE 15.2:

FIGURE 15.2:


If you haven’t saved the project yet, you will most likely see the entry Project 1 (which is the default name of the Project) Do not add this reference to your project yet. Rename the project’s components, save them to a ‘new folder then add  the reference. It’s not that you can’t add a reference to Project 1 rather the next time you open the References dialog box, you won’t remember what the Project Class does

5. Then open the test Form’s Code window and enter the lines in Code 15.4.

the c timer test from’s code

DiM TMR As New C Timer ‘
Private Sub Commandl_Click()
. If Commandl.Caption – ‘Start Timing’ Then
TMR.Start Counting
Commandl. Caption Stop Timing”
Commandl.Capt;on = ‘Start Timing’
End If
End Sub
Private Sub Command2_Click()
ETime – TMR.ElapsedTime
MsgBox ‘I’ve been counting’ for· c:. vbc rt.f &
Hour(TMR.·ETime) & • hours’ & vbCrLf & _
Minute(TMR.ETime) & .minute!> and [” vt>CrLf &
Second(TMR. e.Time) s seconds’ & vr-Crt.f
. End Sub
Private Sub Command3_Click()
End Sub

6. Right-click the name of the Test Project in ~h~Project Explorer window and select the command Set as Start Up. Whell you press F5 the Test Form will appear Run the project and exercise the C timer Class Start Timer button !o start the timer. Its caption will change to “Stop timer when you click it again.

it will pause the timing. Stop the timer and restart it again. You can read”the elapsed ‘time at any point, but the reading will be correct only if the timer isn’t running. If you examine the Class’ code you’ll see.that the total interval variable is updated every time you stop the timer.

The Test Project’s Code

Let’s look at the test project’s code. The first line is a declaration of an object variable:
Dim TMR As New C Timer

TMR is an object variable of C timer type. C Timer is the new Class you’ve just .implemented. Just like you need an object variable to access Word’s methods and properties, you need an object variable to access the methods of the C timer Class.
The New keyword tells Visual Basic to create a new instance of the object (you could have used the Create Object function instead, and we’ll discuss this shortly). Your code will access the properties and methods exposed by the C timer Class through the TMR object variable. As soon as you enter the name of the object variable and a period in the code a list of its members will appear in the Code window provided that the Auto List Members feature of the editor is on. I suggest that you turn on this feature when working with Classes. The new Class is registered and Visual Basis:treats it like any other Class.

The code behind the buttons of the application is trivial. The Start/Stop button calls the Start Counting and Stop Counting methods of the Class to start and stop the timing. The Show Interval button reads the value of the Elapsed time property and formats it as hours, minutes, and seconds, and displays it on a message box.

Posted on November 5, 2015 in Building AdiveX Components

Share the Story

Back to Top