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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Why does the callback method of an EventHandler<T> need to be static?

Why does the callback method of an EventHandler<T> need to be static?

Scheduled Pinned Locked Moved C#
questioncsharptutorial
4 Posts 3 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.
  • T Offline
    T Offline
    TMattC
    wrote on last edited by
    #1

    Could someone please explain to me why the callback method of an EventHandler has to be static? According to the .NET documentation the delegate looks like this:

    [SerializableAttribute]
    public delegate void EventHandler(
    Object sender,
    TEventArgs e
    )

    No hint of static there. So, my second question would be; If I want to declare a delegate which should take a static callback method, how to do that?

    Richard DeemingR 1 Reply Last reply
    0
    • T TMattC

      Could someone please explain to me why the callback method of an EventHandler has to be static? According to the .NET documentation the delegate looks like this:

      [SerializableAttribute]
      public delegate void EventHandler(
      Object sender,
      TEventArgs e
      )

      No hint of static there. So, my second question would be; If I want to declare a delegate which should take a static callback method, how to do that?

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      What makes you think that the method needs to be static? :confused:

      public class Foo
      {
      public event EventHandler MyEvent;

      public void Test()
      {
          var handler = MyEvent;
          if (handler != null) handler(this, EventArgs.Empty);
      }
      

      }

      public class Bar
      {
      private readonly Foo _foo;

      public Bar()
      {
          \_foo = new Foo();
          \_foo.MyEvent += MyEventHandler;
      }
      
      public void Test()
      {
          \_foo.Test();
      }
      
      private void MyEventHandler(object sender, EventArgs e)
      {
          // Not static!
          Console.WriteLine("Bacon.");
      }
      

      }

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      T 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        What makes you think that the method needs to be static? :confused:

        public class Foo
        {
        public event EventHandler MyEvent;

        public void Test()
        {
            var handler = MyEvent;
            if (handler != null) handler(this, EventArgs.Empty);
        }
        

        }

        public class Bar
        {
        private readonly Foo _foo;

        public Bar()
        {
            \_foo = new Foo();
            \_foo.MyEvent += MyEventHandler;
        }
        
        public void Test()
        {
            \_foo.Test();
        }
        
        private void MyEventHandler(object sender, EventArgs e)
        {
            // Not static!
            Console.WriteLine("Bacon.");
        }
        

        }

        T Offline
        T Offline
        TMattC
        wrote on last edited by
        #3

        Well, Im not sure... I tried to use the EventHandler earlier, got error, and was under the impression that the non-static nature of the callback-method was the cause. So I changed the code declaring an explicit delegate and it worked. But now when I changed back it actually still works. So Im a little confused right now why it didnt work earlier. Anyway. I can clearly see it works with a non-static method now. Just forget I asked :-\ Thanks anyway.

        L 1 Reply Last reply
        0
        • T TMattC

          Well, Im not sure... I tried to use the EventHandler earlier, got error, and was under the impression that the non-static nature of the callback-method was the cause. So I changed the code declaring an explicit delegate and it worked. But now when I changed back it actually still works. So Im a little confused right now why it didnt work earlier. Anyway. I can clearly see it works with a non-static method now. Just forget I asked :-\ Thanks anyway.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Some statical analysis may flag it as "requires static" if you do not reference any non-static members of the class.

          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