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. How to specify an event handler

How to specify an event handler

Scheduled Pinned Locked Moved C#
questioncsharpvisual-studiocomtutorial
2 Posts 2 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.
  • P Offline
    P Offline
    peterchen
    wrote on last edited by
    #1

    If I have a class with an event, and a class that wants to attach to the handler:

    class A
    {
    public event EventHandler SomeEvent;
    }

    class B
    {
    public void AttachToHandler(A a)
    {
    a.SomeEvent += ***; // << here's my question
    }

    void SomeEventHandler(object sender, SomeEventArgs e) {... }

    }

    The VC# Express IDE ("press TAB to insert..") suggests:

    a.SomeEvent += new EventHandler(SomeEventHandler);

    However, the following also seems to work:

    a.SomeEvent += SomeEventHandler;

    Now the questions: Is there a difference between the two? A "preferred style"?


    We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
    My first real C# project | Linkify!|FoldWithUs! | sighist

    M 1 Reply Last reply
    0
    • P peterchen

      If I have a class with an event, and a class that wants to attach to the handler:

      class A
      {
      public event EventHandler SomeEvent;
      }

      class B
      {
      public void AttachToHandler(A a)
      {
      a.SomeEvent += ***; // << here's my question
      }

      void SomeEventHandler(object sender, SomeEventArgs e) {... }

      }

      The VC# Express IDE ("press TAB to insert..") suggests:

      a.SomeEvent += new EventHandler(SomeEventHandler);

      However, the following also seems to work:

      a.SomeEvent += SomeEventHandler;

      Now the questions: Is there a difference between the two? A "preferred style"?


      We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
      My first real C# project | Linkify!|FoldWithUs! | sighist

      M Offline
      M Offline
      mikanu
      wrote on last edited by
      #2

      I've always used the first method (using the New operator to instantiate a new delegate that will handle the event). You can alway declare a variable that holds the reference to the event handler in advance and then use that as your handler like so:
      EventHandler eh; eh = **New** EventHandler(SomeEventHandler); a.SomeEvent += eh;
      this way you could later detach the event handler too, like so: a.SomeEvent -= eh; _//(as long as eh still references your event handler!)_
      The second way seems to be a shortcut for the first (in which the compiler automatically generates the code that instantiated the delegate and than passes it your function). Hope this helps, but your best bet is to do some reading on delegates and event handlers in C#.

      ---- www.digitalGetto.com

      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