Solved

Event handler for objects array

Posted on 2004-07-30
9
321 Views
Last Modified: 2013-11-25
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?


0
Comment
Question by:EyalL
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 13

Accepted Solution

by:
crazyman earned 125 total points
ID: 11674706
Create an interface class IClient
- Add your methods EG

public sub DataArrival(NewData as string,objClient as ClientClass)
'Place no code here
end sub


Implement this interface where you want to get the events ie maybe in your main form.

Implements IClient

you will get this event now

sub IClient_DataArrival(NewData as string,objClient as ClientClass)
'place your code here
'objClient is the clss where the event came from
end sub

Now add this Variable/Property to clientclass
public OwnerInterface as IClient

now when create your objects do the following, and hold them in a collection eg MyCollection

dim objClient as ClientClass
set ObjClient = new ClientClass
set objClient.OwnerInterface = me
MyCollection.add objClient


Now instead of doing Raiseevent in your clientclass do
OwnerInterface.DataArrival(TheData,me)

You will now get events from all classes throught the one IClient_DataArrival with the instance of the class as the objClient parameter...
0
 

Author Comment

by:EyalL
ID: 11675127
thanks crazyman,

I'll try that.

Is there any other simpler way (except of moving to VB.NET)?
0
 
LVL 13

Expert Comment

by:crazyman
ID: 11675143
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...
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11676512
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
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 7

Expert Comment

by:_agj_
ID: 11676639
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
0
 
LVL 7

Expert Comment

by:_agj_
ID: 11676649
i cant see whats limiting here...???
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 11676772
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
0
 
LVL 7

Expert Comment

by:_agj_
ID: 11677099
...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 ;)
0
 
LVL 15

Expert Comment

by:SRigney
ID: 11681722
Not any better, but I have used an array of my own UserControls, and each user control had one instance of my class.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now