Declaring an array of objects WithEventsIn 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: e.g.
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.
Public Sub CaptureError(Source as Object, ErrorCode as Long)
RaiseEvent Error(Source, ErrorCode) End Sub In the source object: 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: 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 TechByteIf 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. Other TechBytes: |
|

