To use a callback object in your server, you must take steps outlined in Step by Step 12.7.
STEP BY STEP
12.7 Using a Callback Object in the Server
-
Declare a Public variable in the Server class where you will be receiving the Callback
object. The variable will be of the same type as the Interface
class and will not be instantiated (see Listing 12.24). You will use this variable as a classwide
pointer to Callback objects that client applications pass to this server (see the following step).
-
Write a method that receives a ByVal parameter whose type is the type of the Interface class.
This parameter holds the callback object that the client is passing to the server. Within
this method, set the Public variable declared in the preceding step to point to the callback
object parameter. Pointing the Public variable to the parameter will make the object in the
parameter available throughout the entire class (see Listing 12.24).
-
Use the Public variable (which now points to the callback object) to call the object's
notification method when your server needs to send a notification to the client. This
will cause the method to run on the client side (see Listing 12.25).
Listing 12.24 shows code in the Server class that will receive a Callback object, as described
in steps 1 and 2. In the General Declarations section, you declare (but don't instantiate
with New) a Public object variable named cbObject whose type is INotification, the same as the Interface
definition that client and server are using for the Callback object class.
A method of the server (named LongProcess in the example) receives a parameter whose type
is INotification (the Interface type). Using
the Set statement, make the Public variable
cbObject point to the parameter. Because cbObject
is a Public variable, it's visible in the entire class, and thus
effectively makes the Callback object parameter available throughout the class.
LISTING 12.24
DECLARING AND RECEIVING THE Callback OBJECT IN THE SERVER
'General Declarations of a server class
Public cbObject As INotification
.
.
Public Sub LongProcess(ByVal cbCurr As _
INotification)
Set cbObject = cbCurr
.
.
.
End Sub
Listing 12.25 shows a line of code elsewhere in the Server class (perhaps just farther down
in the LongProcess method's code). This code calls the Notify
method of the Public object class, cbObject.
LISTING 12.25
USING THE Callback OBJECT'S METHOD TO NOTIFY THE CLIENT
.
.
.
'somewhere in your server class code:
obObject.Notify "The sky is falling!"
.
.
.
Recall from Listing 12.24 and from steps 1 and 2 in the preceding Step by Step that cbObject
really points to the callback object parameter that the client passed to this server when it
called the LongProcess method. Calling this method will therefore run
the code that the client implemented for the callback object, as described in "Implementing
the Callback Object in the Client."
NOTE - Callback Object Should Be Public : Note
the importance of using a Public variable to hold the callback object both in
the server and in the client (see preceding section). If the callback object is
not Public, you will receive a compiler error, because it's illegal to pass a
Private object to a class method and also illegal to set a method's object parameter
to a Private object.
In addition, you must always remember to mark the callback object parameter as
ByVal.