473,714 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1461
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@<REM OVE> wrote in message
news:43******** *************** @news.sunsite.d k...
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.PacketAr rived += DataLink1.Packe tArrivedHandler ;
Radio2.PacketAr rived += DataLink2.Packe tArrivedHandler ;

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.PacketAr rived += DataLink1.Packe tArrivedHandler ;
Radio2.PacketAr rived += DataLink2.Packe tArrivedHandler ;


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.Pac ketArrived' cannot be accessed with an instance reference;
qualify it with a type name instead"...
Well, I can do the followring:
RadioLayer.Pack etArrived += 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.Packet Arrived += DataLink1.Packe tArrivedHandler ;
Radio2.Packet Arrived += DataLink2.Packe tArrivedHandler ;

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.Pac ketArrived' cannot be accessed with an instance reference;
qualify it with a type name instead"...
Well, I can do the followring:
RadioLayer.Pack etArrived += 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(R adio1);
RadioLayer Radio2 = new RadioLayer();
DataLinkLayer DataLink2 = new DataLinkLayer(R adio2);

Constructor:

DataLinkLayer(R adioLayer radio)
{
radio.PacketArr ived += PacketArrivedHa ndler;
}

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(R adio1);
RadioLayer Radio2 = new RadioLayer();
DataLinkLayer DataLink2 = new DataLinkLayer(R adio2);

Constructor:

DataLinkLayer(R adioLayer radio)
{
radio.PacketArr ived += PacketArrivedHa ndler;
}

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 PacketArrivedHa ndler(PacketEve ntArgs packetEvent);

public class RadioLayer

{

public static event PacketArrivedHa ndler PacketArrived;

public static void OnPacketArrived (PacketEventArg s packetEvent)

{

if(PacketArrive d!=null)

PacketArrived(p acketEvent);

}

}

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3072
by: Vince C. | last post by:
Hi. I'd like to trap ADO Recordset object events in my ASP script (either VBS or JS, no preference). I've tried (in VBS) writing a Sub rs_RecordChangeComplete( adReason, cRecords, pError, adStatus, pRecordset ) - rs being a server-side ADODB.Recordset object - but it doesn't get called whenever rs is moved (I browse it in a loop). Is there a way to catch those events from ASP?
5
73211
by: RAJ | last post by:
hi plz tell me how to know "how window is going to close"... i have to right code for X button of forms... plz telll me thanks bye
2
2509
by: COLIN JACK | last post by:
Hi All, I've got a situation where I'm implementing an interface (BaseInterface in example below) and I want to use explicity interface implementation of an event so that I can add type safety. To see what I mean look at the example below where the class implementing the interface actually wants the event to be for a more specific delegate. Now this seems to work but the code, to me is unnecessarily ugly. This leaves me wondering if...
11
437
by: pemo | last post by:
Ambiguous? I have a student who's asked me to explain the following std text (esp. the footnote). 6.2.6.1.5 Certain object representations need not represent a value of the object type. If the stored value of an object has such a representation and is read by an lvalue
4
4500
by: ABC | last post by:
I want to check the form's controls have or not the specific properties or events. How to determine or gather the properties list under the run-time environment?
10
2355
by: pemo | last post by:
As far as I understand it, a trap representation means something like - an uninitialised automatic variable might /implicitly/ hold a bit-pattern that, if read, *might* cause a 'trap' (I'm not sure what 'a trap' means here - anyone?). I also read into this that, an *initialised* automatic variable, may never hold a bit pattern that might, when read, cause a 'trap'. I.e., if an auto is explicitly initialised, it may *never* hold a...
0
4074
by: Bill Schmidt | last post by:
I am attempting to send an SNMP trap using C#. however I have been unable to find a way to do it. websearching has turned up a small number of 3rd party tools that should be able to do this, however the ones that are available for use without purchase are tend to be poorly documented and I am unable to find a trap method. I have read that this should be able to be done with WMI using a management object, however once again all thats...
7
2341
by: mavigozler | last post by:
IE7 does not appear to set an event on contained text inside SPAN elements whose 'onclick', 'onmouseover', and 'onmouseout' events, defying the HTML recommendation. Firefox appears to conform. Is that so?
1
4905
by: swethak | last post by:
Hi, I am desiging the calendar application for that purpose i used the below code. But it is for only displys calendar. And also i want to add the events to calendar. In that code displys the events when click on that date that perticular event displyed in a text box.But my requirement is to when click on that date that related event displyed in same td row not the text box. and also i add the events to that calendar.plz advice how to...
0
8711
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9017
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4464
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4726
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3158
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2111
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.