Link to home
Start Free TrialLog in
Avatar of EyalL
EyalL

asked on

Event handler for objects array

I have a class module called ClientClass that receive data from winsock control and has a DataReceived event. Another class - AppClass uses the ClientClass this way:

private WithEvents Clients() as ClientClass

private sub Class_Initializate()

     redim Clients (ValueFromDatabase)

End Sub



How do I implement one event handler function in the AppClass for all the Clients?


ASKER CERTIFIED SOLUTION
Avatar of crazyman
crazyman
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of EyalL
EyalL

ASKER

thanks crazyman,

I'll try that.

Is there any other simpler way (except of moving to VB.NET)?
There is another way, without using the interface but it is a similar method.
Create a class module that has the events in, create methods that simply raise the events, have one instance declared withevents and hold a ref to that instance from each class, then call the methods from each class passing through the instance, it is pretty much the same just using a instance of a class instead of an interface...
Avatar of Mike Tomlinson
VB.Net will elegantly solve your event handling problem via the AddHandler() function but it has no equivalent of the Winsock Control so you would have to rebuild ALL of your communication code from scratch using a lower level methodology.

Idle_Mind
private WithEvents Clients() as ClientClass

create an Event MyEvent in your ClientClass.
on DataReceived event of the winsock, raise ur MyEvent.

in ur AppClass u shud get this event with its index in the array.

so u can just code a:

sub Clients_MyEvent(indx as integer)
'the data has arrived for clients(indx)
clients(indx).dourstuff

end sub
i cant see whats limiting here...???
The limiting factor is that you didn't try your own code out!  From the help file:

You should be aware of the following limitations on the use of WithEvents variables:
     A WithEvents variable cannot be a generic object variable. That is, you cannot declare it As Object — you must specify the class name when you declare the variable.
     You cannot declare a WithEvents variable As New. The event source object must be explicitly created and assigned to the WithEvents variable.
     You cannot declare WithEvents variables in a standard module. You can declare them only in class modules, form modules, and other modules that define classes.
     You cannot create arrays of WithEvents variables.

VB immediately flags the line with a compile error in the IDE.

Regards,

Idle_Mind
...idle_mind u are right...
i was assuming the array of withevents would work(though i havent seen it wrk..except for arrays of controls)...

looks more interesting now ;)
Not any better, but I have used an array of my own UserControls, and each user control had one instance of my class.