Can I raise an event for a class rather than an object?
-
I have object A, operating in the program's main thread, which creates several copies of object B (B1, B2, etc) and executes them in several threads. I have figured out how to get A to raise a custom event that B2 should deal with. Since A knows all about the objects it created, it simply calls
B2.OnBEvent()
. However, I now want a B object (say B3) to raise an event for A to deal with. B3 does not know about the object A (no reference to A has been passed to B3), so it can't callA.OnAEvent()
. My question is, can I declare an OnAEvent() method to be static, so that B3 does not have to know about object A? Can I callClassA.OnAEvent()
and have that event handled by object A? BTW: in the on-going conflict between how threading is done and how Clive Pottinger thinks threading is done, the score now stands at 43 - 0.Clive Pottinger Victoria, BC
-
I have object A, operating in the program's main thread, which creates several copies of object B (B1, B2, etc) and executes them in several threads. I have figured out how to get A to raise a custom event that B2 should deal with. Since A knows all about the objects it created, it simply calls
B2.OnBEvent()
. However, I now want a B object (say B3) to raise an event for A to deal with. B3 does not know about the object A (no reference to A has been passed to B3), so it can't callA.OnAEvent()
. My question is, can I declare an OnAEvent() method to be static, so that B3 does not have to know about object A? Can I callClassA.OnAEvent()
and have that event handled by object A? BTW: in the on-going conflict between how threading is done and how Clive Pottinger thinks threading is done, the score now stands at 43 - 0.Clive Pottinger Victoria, BC
-
I have object A, operating in the program's main thread, which creates several copies of object B (B1, B2, etc) and executes them in several threads. I have figured out how to get A to raise a custom event that B2 should deal with. Since A knows all about the objects it created, it simply calls
B2.OnBEvent()
. However, I now want a B object (say B3) to raise an event for A to deal with. B3 does not know about the object A (no reference to A has been passed to B3), so it can't callA.OnAEvent()
. My question is, can I declare an OnAEvent() method to be static, so that B3 does not have to know about object A? Can I callClassA.OnAEvent()
and have that event handled by object A? BTW: in the on-going conflict between how threading is done and how Clive Pottinger thinks threading is done, the score now stands at 43 - 0.Clive Pottinger Victoria, BC
What you are describing are not really events, but method calls. Take a look at C#
event
s anddelegate
s, any documentation you find on them will show you how to use them. When A creates instances of Bs, it can register itself with B events and visa versa. Only set up static events on A if it makes sense to do so. Most often it will make more sense for the instance to process events. Just google "C# event", I'm sure that will get you plenty of starting points. Hope that helps.
Try code model generation tools at BoneSoft.com.
-
I have object A, operating in the program's main thread, which creates several copies of object B (B1, B2, etc) and executes them in several threads. I have figured out how to get A to raise a custom event that B2 should deal with. Since A knows all about the objects it created, it simply calls
B2.OnBEvent()
. However, I now want a B object (say B3) to raise an event for A to deal with. B3 does not know about the object A (no reference to A has been passed to B3), so it can't callA.OnAEvent()
. My question is, can I declare an OnAEvent() method to be static, so that B3 does not have to know about object A? Can I callClassA.OnAEvent()
and have that event handled by object A? BTW: in the on-going conflict between how threading is done and how Clive Pottinger thinks threading is done, the score now stands at 43 - 0.Clive Pottinger Victoria, BC
A should register the events it wants to listen to on B when it creates them.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway -
I have object A, operating in the program's main thread, which creates several copies of object B (B1, B2, etc) and executes them in several threads. I have figured out how to get A to raise a custom event that B2 should deal with. Since A knows all about the objects it created, it simply calls
B2.OnBEvent()
. However, I now want a B object (say B3) to raise an event for A to deal with. B3 does not know about the object A (no reference to A has been passed to B3), so it can't callA.OnAEvent()
. My question is, can I declare an OnAEvent() method to be static, so that B3 does not have to know about object A? Can I callClassA.OnAEvent()
and have that event handled by object A? BTW: in the on-going conflict between how threading is done and how Clive Pottinger thinks threading is done, the score now stands at 43 - 0.Clive Pottinger Victoria, BC
cpotting wrote:
so that B3 does not have to know about object A?
Yeah that's a very common problem in software design.
cpotting wrote:
Can I call
There may be many things you can do, but likely a sub set of them that are considered best practice. You could do some studying of Software Design Patterns that would shed some light on this subject for you, if you wanted to.
led mike
-
Perhaps make it a static event and make the "child" classes register for the event when they're created?
That is what I was asking - is setting up a static event possible. I know I could simply pass a reference to A to B1, B2, B3, etc. Then the B's would have no problem raising an event for A to deal with. I was just wondering if there is a way to do this without having to pass that reference to A. I'll see if I can find out how to create a static event. I haven't yet run across examples or documentation showing that being done.
Clive Pottinger Victoria, BC
-
What you are describing are not really events, but method calls. Take a look at C#
event
s anddelegate
s, any documentation you find on them will show you how to use them. When A creates instances of Bs, it can register itself with B events and visa versa. Only set up static events on A if it makes sense to do so. Most often it will make more sense for the instance to process events. Just google "C# event", I'm sure that will get you plenty of starting points. Hope that helps.
Try code model generation tools at BoneSoft.com.
Actually, I was talking about the events. In my examples I was just showing the calls to the methods that raise the events for simplicity. It would seem, from your reply, that it is possible to set up a static event the class A. Whether or not that is better is better than passing A to the B's so they can register the event...? I'll try and see.
Clive Pottinger Victoria, BC
-
Actually, I was talking about the events. In my examples I was just showing the calls to the methods that raise the events for simplicity. It would seem, from your reply, that it is possible to set up a static event the class A. Whether or not that is better is better than passing A to the B's so they can register the event...? I'll try and see.
Clive Pottinger Victoria, BC
I apologize for the assumption, from your post I wasn't sure you were actually using events. I don't think you'll need static events, unless that's what you want to do. If B has an event, when A creates those instances it should be able to hook the new B instance's events to a method of it's own. Also on creation of the B instances, you can set them up as subscribers to events that A has. Is this the kind of thing you're talking about? Or did I completely misunderstand your question?
class A {
// A has an event to notify B instances with
public event EventHandler OnA;public void Start() { B b1 = new B(1); // hook B event to my method b1.OnB += new EventHandler(CatchOnB); // hook B method to my event OnA += new EventHandler(b1.CatchOnA); B b2 = new B(2); b2.OnB += new EventHandler(CatchOnB); OnA += new EventHandler(b2.CatchOnA); b1.DoStuff(); b2.DoStuff(); if (OnA != null) { OnA(this, EventArgs.Empty); } } private void CatchOnB(object sender, EventArgs e) { Console.WriteLine("B{0} called me", ((B)sender).Id); }
}
class B {
public event EventHandler OnB;public int Id; public B(int id) { this.Id = id; } public void CatchOnA(object sender, EventArgs e) { Console.WriteLine("A called B{0}", Id); } public void DoStuff() { if (OnB != null) { OnB(this, EventArgs.Empty); } }
}
Try code model generation tools at BoneSoft.com.
-
That is what I was asking - is setting up a static event possible. I know I could simply pass a reference to A to B1, B2, B3, etc. Then the B's would have no problem raising an event for A to deal with. I was just wondering if there is a way to do this without having to pass that reference to A. I'll see if I can find out how to create a static event. I haven't yet run across examples or documentation showing that being done.
Clive Pottinger Victoria, BC
public class A
{
public static event EventHandler<YourEventArgs> GlobalEvent;
public A()
{
// Attach a handler to the event
A.GlobalEvent += new EventHandler<YourEventArgs>(delegate void (YourEventArgs e)
{
// Do what you want to here when the single event is fired to trigger all events.
});
}
} -
I apologize for the assumption, from your post I wasn't sure you were actually using events. I don't think you'll need static events, unless that's what you want to do. If B has an event, when A creates those instances it should be able to hook the new B instance's events to a method of it's own. Also on creation of the B instances, you can set them up as subscribers to events that A has. Is this the kind of thing you're talking about? Or did I completely misunderstand your question?
class A {
// A has an event to notify B instances with
public event EventHandler OnA;public void Start() { B b1 = new B(1); // hook B event to my method b1.OnB += new EventHandler(CatchOnB); // hook B method to my event OnA += new EventHandler(b1.CatchOnA); B b2 = new B(2); b2.OnB += new EventHandler(CatchOnB); OnA += new EventHandler(b2.CatchOnA); b1.DoStuff(); b2.DoStuff(); if (OnA != null) { OnA(this, EventArgs.Empty); } } private void CatchOnB(object sender, EventArgs e) { Console.WriteLine("B{0} called me", ((B)sender).Id); }
}
class B {
public event EventHandler OnB;public int Id; public B(int id) { this.Id = id; } public void CatchOnA(object sender, EventArgs e) { Console.WriteLine("A called B{0}", Id); } public void DoStuff() { if (OnB != null) { OnB(this, EventArgs.Empty); } }
}
Try code model generation tools at BoneSoft.com.
Thank you, BoneSoft. I had missed this post way back when, and only just saw it today. This answers my question nicely. I wish I had noticed it before.
Clive Pottinger Victoria, BC
-
Thank you, BoneSoft. I had missed this post way back when, and only just saw it today. This answers my question nicely. I wish I had noticed it before.
Clive Pottinger Victoria, BC
Sure thing. Hope it helps.
Visit BoneSoft.com for code generation tools (XML & XSD -> C#, VB, etc...) and some free developer tools as well.