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. event handler

event handler

Scheduled Pinned Locked Moved C#
comhelp
9 Posts 6 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.
  • N Offline
    N Offline
    nelsonpaixao
    wrote on last edited by
    #1

    Hi, When should i use this:

    protected virtual void OnEvent_PageMenu_Info(string msg)
    {
    if (Event_PageMenu_Info != null)
    { Event_PageMenu_Info(msg); }
    }

    and this:

    Event_PageMenu_Info(msg);

    Sometimes i use one or the other but dont know why :laugh: Thanks :-D

    nelsonpaixao@yahoo.com.br trying to help & get help

    M P D N 4 Replies Last reply
    0
    • N nelsonpaixao

      Hi, When should i use this:

      protected virtual void OnEvent_PageMenu_Info(string msg)
      {
      if (Event_PageMenu_Info != null)
      { Event_PageMenu_Info(msg); }
      }

      and this:

      Event_PageMenu_Info(msg);

      Sometimes i use one or the other but dont know why :laugh: Thanks :-D

      nelsonpaixao@yahoo.com.br trying to help & get help

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Assuming Event_PageMenu_Info is an event.... What happens if you raise the event before checking for null and there's nobody subscribed to the event? NULL reference exception :) If you've implemented a OnEvent_PageMenu_Info(), then that should be used to raise te event. Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      realJSOPR 1 Reply Last reply
      0
      • N nelsonpaixao

        Hi, When should i use this:

        protected virtual void OnEvent_PageMenu_Info(string msg)
        {
        if (Event_PageMenu_Info != null)
        { Event_PageMenu_Info(msg); }
        }

        and this:

        Event_PageMenu_Info(msg);

        Sometimes i use one or the other but dont know why :laugh: Thanks :-D

        nelsonpaixao@yahoo.com.br trying to help & get help

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Yeah, as Mark said. Plus wrapping it in a method allows derived classes to raise the event as well.

        1 Reply Last reply
        0
        • N nelsonpaixao

          Hi, When should i use this:

          protected virtual void OnEvent_PageMenu_Info(string msg)
          {
          if (Event_PageMenu_Info != null)
          { Event_PageMenu_Info(msg); }
          }

          and this:

          Event_PageMenu_Info(msg);

          Sometimes i use one or the other but dont know why :laugh: Thanks :-D

          nelsonpaixao@yahoo.com.br trying to help & get help

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          Everything the others have said. Also, Microsoft recommend (and use themselves) a copy to handle the possibility of the event becoming null after the null check but before it's raised. This is the way I implement something like this:

          using System;

          public class MyPage
          {
          // use EventHandler<TEventArgs> for maintenance simplicity
          public event EventHandler<PageMenuInfoChangedEventArgs> PageMenuInfoChanged;
          // private variable
          private object _PageMenuInfo;
          // public property
          public object PageMenuInfo
          {
          get { return _PageMenuInfo; }
          set
          {
          if (_PageMenuInfo != value)
          {
          _PageMenuInfo = value;
          // pass event args to method
          OnPageMenuInfoChanged(new PageMenuInfoChangedEventArgs("Changed!"));
          }
          }
          }
          protected virtual void OnPageMenuInfoChanged(PageMenuInfoChangedEventArgs e)
          {
          // copy event
          EventHandler<PageMenuInfoChangedEventArgs> eh = PageMenuInfoChanged;
          if (eh != null)
          // raise event using standard pattern
          eh(this, e);
          }
          }
          public class PageMenuInfoChangedEventArgs : EventArgs
          {
          public PageMenuInfoChangedEventArgs(string message)
          {
          _Message = message;
          }
          private string _Message;
          public string Message
          {
          get { return _Message; }
          }
          }

          Dave
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
          Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

          1 Reply Last reply
          0
          • M Mark Salsbery

            Assuming Event_PageMenu_Info is an event.... What happens if you raise the event before checking for null and there's nobody subscribed to the event? NULL reference exception :) If you've implemented a OnEvent_PageMenu_Info(), then that should be used to raise te event. Mark

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #5

            He could add a do-nothing event handler inside the class that throws the event so that the invocation list is never empty.

            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

            N 1 Reply Last reply
            0
            • realJSOPR realJSOP

              He could add a do-nothing event handler inside the class that throws the event so that the invocation list is never empty.

              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

              N Offline
              N Offline
              Nicholas Butler
              wrote on last edited by
              #6

              I found a neat trick recently - can't remember where. You can add a dummy handler when declaring the event:

              public event EventHandler<...EventArgs> MyEvent = delegate { };

              Then the event is never null. Nick

              ---------------------------------- Be excellent to each other :)

              realJSOPR D 2 Replies Last reply
              0
              • N Nicholas Butler

                I found a neat trick recently - can't remember where. You can add a dummy handler when declaring the event:

                public event EventHandler<...EventArgs> MyEvent = delegate { };

                Then the event is never null. Nick

                ---------------------------------- Be excellent to each other :)

                realJSOPR Offline
                realJSOPR Offline
                realJSOP
                wrote on last edited by
                #7

                ooooohhhhhh - good tip. That deserves a 5. :)

                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                1 Reply Last reply
                0
                • N Nicholas Butler

                  I found a neat trick recently - can't remember where. You can add a dummy handler when declaring the event:

                  public event EventHandler<...EventArgs> MyEvent = delegate { };

                  Then the event is never null. Nick

                  ---------------------------------- Be excellent to each other :)

                  D Offline
                  D Offline
                  DaveyM69
                  wrote on last edited by
                  #8

                  Excellent - nice tip Nick - 5'd :-D

                  Dave
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                  Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                  1 Reply Last reply
                  0
                  • N nelsonpaixao

                    Hi, When should i use this:

                    protected virtual void OnEvent_PageMenu_Info(string msg)
                    {
                    if (Event_PageMenu_Info != null)
                    { Event_PageMenu_Info(msg); }
                    }

                    and this:

                    Event_PageMenu_Info(msg);

                    Sometimes i use one or the other but dont know why :laugh: Thanks :-D

                    nelsonpaixao@yahoo.com.br trying to help & get help

                    N Offline
                    N Offline
                    nelsonpaixao
                    wrote on last edited by
                    #9

                    Thanks everone for the posts :-D

                    nelsonpaixao@yahoo.com.br trying to help & get help

                    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