Declaring an array of objects WithEvents

In VB it is not possible to declare an array of objects WithEvents. One possible workaround is to use an "intermediary object" to trap and forward events between the array of "source objects" that will generate the events and the "event handler" (e.g. the core application).

To create the intermediary:
- Create a new ActiveX EXE project and add a Class Module.
- In the Class Module create events which duplicate the array object events you want to trap and include an additional parameter to specify the Source object.

e.g.
To pass the event Widget_Error(ErrorCode as Long) for an array of Widget objects, create the following event in the class:

Public Event Error(Source as Obect, ErrorCode as Long)

- Next, create public methods which correspond to the Events that the intermediary will handle to capture the events. These subroutines should RaiseEvent on the corresponding event.

e.g.
For the Widget_Error event:

Public Sub CaptureError(Source as Object, ErrorCode as Long)
    RaiseEvent Error(Source, ErrorCode)
End Sub

In the source object:
- If you do not have access to the source code of the object (e.g. a standard VB object) you will need to create a wrapper ActiveX EXE or DLL.
- Depending on the requirements of your project, it may be helpful to create a unique identifier Property (ID/Index/Key/etc.) in the source object.
- Add the intermediary ActiveX EXE to the References for the project.
- Create an intermediary object.
- Create a Property in which you can specify a pointer to the intermediary object. The intermediary will be shared with the event handler to allow messages to be passed between them.

e.g.

Private objIntermediary As Intermediary.Class

Public Property Set Intermediary(ByVal IntermedObject As Intermediary.Class)
    Set wanAnalyst = IntermedObject
End Property

- Trigger the intermediary object's event capturing methods when you want an event fired in the event handler, passing a self-reference to the source object ("Me") in the call.

e.g.

objIntermediary.CaptureError Me, 102949

From the event handler:
- Add References to the source object and the intermediary object types to the project.
- Create an intermediary object WithEvents in the project component that will handle the events.
- Create the array of source objects for which you will trap events (e.g. the Widget objects). As each object is created the Intermediary property must be set to the intermediary object created. Depending on your needs, if you have created a unique identifier property for the object it may be useful to specify at this time to distinguish the source objects as events are fired.
- Events from the source objects are then captured through the Intermediary object's events. They are distinguised by the Source parameter passed in the event.

e.g.

Private WithEvents objIntermediary As Intermediary.Class

Sub MakeObjectArray()
    For i = 1 to 10 ' create 10 source objects
        With objSource(i)
            Set .Intermediary = objIntermediary ' set event messenger
            .ID = i ' set unique ID
        End With
    Next
End Sub

Private Sub objIntermediary_Error(Source As Object, ErrorCode As Long)
    MsgBox "Error " & ErrorCode & " was raised by source object " & Source.ID & "!"
End Sub


Author: ASAK
Created: Oct 9 2005 (last modified Nov 30 2005)
Categories: Visual Basic
TechByte #49

Warning: By visiting this site and/or by using any information contained herein, you agree to the Techbytes.ca terms of use.



Add a comment about this TechByte

If you wish to add a comment regarding this TechByte, please use the form below. Please note that by submitting comments using this form you are allowing all of the information submitted to be visible on this website. Any comments submitted using this form will only be shown on the website if they are approved by the administrators of this site. IF APPROVED, COMMENTS MAY TAKE SEVERAL DAYS TO BE POSTED.

Posted By: (Optional)

Comments:


Other TechBytes: