471,354 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

Trap Events in a specific object...

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
Nov 29 '05 #1
7 1388
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.

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"Thorbjørn Jørgensen softdoc.dk>" <thorbjorn@<REMOVE> wrote in message
news:43***********************@news.sunsite.dk...
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


Nov 29 '05 #2
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
Nov 29 '05 #3

Thorbjorn Jorgensen wrote:
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?


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.

--
Larry Lard
Replies to group please

Nov 29 '05 #4
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
Nov 29 '05 #5
Thorbjørn Jørgensen <thorbjorn@ wrote:
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:

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
Nov 29 '05 #6
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
Nov 29 '05 #7
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


Nov 29 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by COLIN JACK | last post: by
11 posts views Thread by pemo | last post: by
10 posts views Thread by pemo | last post: by
reply views Thread by Bill Schmidt | last post: by
1 post views Thread by swethak | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.