Trap Events in a specific object...

  • Thread starter Thread starter Thorbjørn Jørgensen
  • Start date Start date
T

Thorbjørn Jørgensen

Hi

I am currently creating a WLAN simulation problem and have encountered a
problem...

I have two classes (Channel and RadioLayer), and for instance create the
following objects:

Channel channel = new Channel();

RadioLayer Radio1 = new RadioLayer();

RadioLayer Radio2 = new RadioLayer();

What I would like to do is to raise an Event in the channel object (no
problem) and only trap this event in Radio1 and not in Radio2 (the
problem!)... Is there a nice way to do this?

I am trying to use events as an interface between the layers, because it
makes it much easier to create new components with new functionality, but it
is causing me some problems. A solution could be to raise the event, and
then parse the event for relevance in all of the RadioLayer object, but
since there can be a lot of those, I think it will degrade my performance a
lot.

I am currently using VS2003, so is there improvements that will help me in
this situation in dotnet 2.0 (VS2005)?



Is there another smart way to solve the problem?



I am looking forward to your comments...



Regards...

Thorbjørn
 
Hi,

You have to subscribe to events explicitly in C#. Therefore, it is you who
chooses whether to listen to a particular event or not. For example, you can
pass a flag to the RadioLayer's constructor to indicate whether this
instance of RadioLayer should subscribe to the underlying channel's events.
 
Hi
You have to subscribe to events explicitly in C#. Therefore, it is you who
chooses whether to listen to a particular event or not. For example, you
can pass a flag to the RadioLayer's constructor to indicate whether this
instance of RadioLayer should subscribe to the underlying channel's
events.

I might just have explained this badly and given a not so good example...
The followring example might be better...
I have an RadioLayer and a DataLinkLayer class, and create the followring
object.
RadioLayer Radio1 = new RadioLayer();
DataLinkLayer DataLink1 = new DataLinkLayer();
RadioLayer Radio2 = new RadioLayer();
DataLinkLayer DataLink2 = new DataLinkLayer();

What I would like to happen, is that Radio1 can raise an event (for instance
when a packet arrive) and that this event only will be traped by DataLink1
and not DataLink2... And when Radio2 raise an event that will only be traped
by DataLink2...
Is there a way to do this?

Regards
Thorbjorn
 
Thorbjorn said:
Hi


I might just have explained this badly and given a not so good example...
The followring example might be better...
I have an RadioLayer and a DataLinkLayer class, and create the followring
object.
RadioLayer Radio1 = new RadioLayer();
DataLinkLayer DataLink1 = new DataLinkLayer();
RadioLayer Radio2 = new RadioLayer();
DataLinkLayer DataLink2 = new DataLinkLayer();

What I would like to happen, is that Radio1 can raise an event (for instance
when a packet arrive) and that this event only will be traped by DataLink1
and not DataLink2... And when Radio2 raise an event that will only be traped
by DataLink2...
Is there a way to do this?

Dmytro did in fact answe your question. You haven't given any
information about how these objects know about each other, so we must
assume that you are hooking up events manually. Doing this, only those
event handlers you *explicitly* hook up will respond to events
happening.

So if RadioLayer has an event called PacketArrived, and you do this:

Radio1.PacketArrived += DataLink1.PacketArrivedHandler;
Radio2.PacketArrived += DataLink2.PacketArrivedHandler;

then yes, datalink2 will respond to packets on radio2, and datalink1
will respond to packets on radio1. No other handlers will respond to
these events - because you haven't asked them to! And if you *don't*
execute these lines (or something like them), then nothing at all will
happen when packets arrive.

Events aren't like, say, Windows broadcast messages, which get sent to
everyone whether they have asked for them or not. Listeners must
*subscribe* to events in order to be informed when the event fires.
 
Hi
First of all, thank you both very much for your replies. I do not doubt that
you both ansewered my question perfectly... I just did not understand the
answer, and tried again... and still got some questions...
Dmytro did in fact answe your question. You haven't given any
information about how these objects know about each other, so we must
assume that you are hooking up events manually. Doing this, only those
event handlers you *explicitly* hook up will respond to events
happening.

So if RadioLayer has an event called PacketArrived, and you do this:

Radio1.PacketArrived += DataLink1.PacketArrivedHandler;
Radio2.PacketArrived += DataLink2.PacketArrivedHandler;

Well I tried this, but can not make it work...
I can see that I have created the RadioLayer with the PacketArrived
correctly, since the event i shown in the Class viewer, as an Event. But
when I try to do the above it gets an error: "Static member
'RadioLayer.PacketArrived' cannot be accessed with an instance reference;
qualify it with a type name instead"...
Well, I can do the followring:
RadioLayer.PacketArrived += Some function...
But then all events created in all RadioLayer obejct will create an event
that is traped by the listener...

Is this because I use VS2003 or have I just made some stupid error?

Regards
Thorbjørn
 
Thorbjørn Jørgensen <thorbjorn@ said:
Hi
First of all, thank you both very much for your replies. I do not doubt that
you both ansewered my question perfectly... I just did not understand the
answer, and tried again... and still got some questions...




Well I tried this, but can not make it work...
I can see that I have created the RadioLayer with the PacketArrived
correctly, since the event i shown in the Class viewer, as an Event. But
when I try to do the above it gets an error: "Static member
'RadioLayer.PacketArrived' cannot be accessed with an instance reference;
qualify it with a type name instead"...
Well, I can do the followring:
RadioLayer.PacketArrived += Some function...
But then all events created in all RadioLayer obejct will create an event
that is traped by the listener...

Is this because I use VS2003 or have I just made some stupid error?

Regards
Thorbjørn

Thorbjørn:

I only know C++, but I'm sure it's similar:

RadioLayer Radio1 = new RadioLayer();
DataLinkLayer DataLink1 = new DataLinkLayer(Radio1);
RadioLayer Radio2 = new RadioLayer();
DataLinkLayer DataLink2 = new DataLinkLayer(Radio2);

Constructor:

DataLinkLayer(RadioLayer radio)
{
radio.PacketArrived += PacketArrivedHandler;
}

It's just standard OOP communication.

David Wilkinson
 
Hi
I only know C++, but I'm sure it's similar:

RadioLayer Radio1 = new RadioLayer();
DataLinkLayer DataLink1 = new DataLinkLayer(Radio1);
RadioLayer Radio2 = new RadioLayer();
DataLinkLayer DataLink2 = new DataLinkLayer(Radio2);

Constructor:

DataLinkLayer(RadioLayer radio)
{
radio.PacketArrived += PacketArrivedHandler;
}

It's just standard OOP communication.
I think that I might have created the Events wrongly, cause if I try the
same with a System.Windows.Forms.Button object I can access events in the
way described... but if I try with the classes I created I can not access
the events when an object is instanced...

This is the basic functionality of the RadioLayer class:
public class PacketEventArgs : EventArgs

{

public PacketEventArgs(Packet packet)

{

}

public Packet packet;

}

public delegate void PacketArrivedHandler(PacketEventArgs packetEvent);

public class RadioLayer

{

public static event PacketArrivedHandler PacketArrived;

public static void OnPacketArrived(PacketEventArgs packetEvent)

{

if(PacketArrived!=null)

PacketArrived(packetEvent);

}

}

But if I create a RadioLayer object, I can not access the PacketArrived
event with the VS autocomplete... What have I done wrong in this code?

Regards

Thorbjørn
 
Hi

Thank you all for you answers...

I have found the error... I was a little too quickly to copy from an example
and had made the Event static, hence the entire problem...



I am sorry to have been interrupted you with this silly question :-)



Regards

Thorbjørn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top