Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Can I raise an event for a class rather than an object?

Can I raise an event for a class rather than an object?

Scheduled Pinned Locked Moved C#
questiontutorial
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Clive D Pottinger

    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 call A.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 call ClassA.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

    E Offline
    E Offline
    Ed Poore
    wrote on last edited by
    #2

    Perhaps make it a static event and make the "child" classes register for the event when they're created?

    C 1 Reply Last reply
    0
    • C Clive D Pottinger

      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 call A.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 call ClassA.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

      B Offline
      B Offline
      BoneSoft
      wrote on last edited by
      #3

      What you are describing are not really events, but method calls. Take a look at C# events and delegates, 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.

      C 1 Reply Last reply
      0
      • C Clive D Pottinger

        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 call A.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 call ClassA.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

        E Offline
        E Offline
        Ennis Ray Lynch Jr
        wrote on last edited by
        #4

        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

        1 Reply Last reply
        0
        • C Clive D Pottinger

          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 call A.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 call ClassA.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

          L Offline
          L Offline
          led mike
          wrote on last edited by
          #5

          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

          1 Reply Last reply
          0
          • E Ed Poore

            Perhaps make it a static event and make the "child" classes register for the event when they're created?

            C Offline
            C Offline
            Clive D Pottinger
            wrote on last edited by
            #6

            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

            E 1 Reply Last reply
            0
            • B BoneSoft

              What you are describing are not really events, but method calls. Take a look at C# events and delegates, 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.

              C Offline
              C Offline
              Clive D Pottinger
              wrote on last edited by
              #7

              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

              B 1 Reply Last reply
              0
              • C Clive D Pottinger

                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

                B Offline
                B Offline
                BoneSoft
                wrote on last edited by
                #8

                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.

                C 1 Reply Last reply
                0
                • C Clive D Pottinger

                  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

                  E Offline
                  E Offline
                  Ed Poore
                  wrote on last edited by
                  #9

                  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.
                         });
                      }
                  }

                  1 Reply Last reply
                  0
                  • B BoneSoft

                    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.

                    C Offline
                    C Offline
                    Clive D Pottinger
                    wrote on last edited by
                    #10

                    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

                    B 1 Reply Last reply
                    0
                    • C Clive D Pottinger

                      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

                      B Offline
                      B Offline
                      BoneSoft
                      wrote on last edited by
                      #11

                      Sure thing. Hope it helps.


                      Visit BoneSoft.com for code generation tools (XML & XSD -> C#, VB, etc...) and some free developer tools as well.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups