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. Generic Change Event handler

Generic Change Event handler

Scheduled Pinned Locked Moved C#
workspace
6 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.
  • D Offline
    D Offline
    dwolver
    wrote on last edited by
    #1

    Lets say I'm using the vs2008 convention of setting up properties and have a Base class and inherited classes. Is there a way in my base class that I can attach event handler to properties the child and base may have to determine if the set method of a property get used. Looking for something to add to base class that would setup the notification. eg static enum State (isUnchanged,isNew,isModified,isDeleted) class MyBase { public State myState; } class MyChild : MyBase { public string FirstName {get;set;} public string LastName {get;set;} }

    D D 2 Replies Last reply
    0
    • D dwolver

      Lets say I'm using the vs2008 convention of setting up properties and have a Base class and inherited classes. Is there a way in my base class that I can attach event handler to properties the child and base may have to determine if the set method of a property get used. Looking for something to add to base class that would setup the notification. eg static enum State (isUnchanged,isNew,isModified,isDeleted) class MyBase { public State myState; } class MyChild : MyBase { public string FirstName {get;set;} public string LastName {get;set;} }

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

      You can do something like this but you'll have to set the state in the setter for each property.

      class MyBase
      {
          public event EventHandler StateChanged;
          private State state;
          public State State
          {
              get { return state; }
              set
              {
                  if (state != value)
                  {
                      state = value;
                      OnStateChanged(EventArgs.Empty);
                  }
              }
          }
          protected virtual void OnStateChanged(EventArgs e)
          {
              EventHandler eh = StateChanged;
              if (eh != null)
                  eh(this, e);
          }
      }
      

      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)

      D 1 Reply Last reply
      0
      • D DaveyM69

        You can do something like this but you'll have to set the state in the setter for each property.

        class MyBase
        {
            public event EventHandler StateChanged;
            private State state;
            public State State
            {
                get { return state; }
                set
                {
                    if (state != value)
                    {
                        state = value;
                        OnStateChanged(EventArgs.Empty);
                    }
                }
            }
            protected virtual void OnStateChanged(EventArgs e)
            {
                EventHandler eh = StateChanged;
                if (eh != null)
                    eh(this, e);
            }
        }
        

        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)

        D Offline
        D Offline
        dwolver
        wrote on last edited by
        #3

        Not quite where I was heading, My baseclass will have no idea what properties the child class has so in pseduo code I'm wondering if something like this is possible The idea is to have a means of setting BaseClass protected void SetupHandlers() foreach property in MyProperties AttachSetChangeEvent () public BaseClass() { SetupHandlers(); }

        D S 2 Replies Last reply
        0
        • D dwolver

          Lets say I'm using the vs2008 convention of setting up properties and have a Base class and inherited classes. Is there a way in my base class that I can attach event handler to properties the child and base may have to determine if the set method of a property get used. Looking for something to add to base class that would setup the notification. eg static enum State (isUnchanged,isNew,isModified,isDeleted) class MyBase { public State myState; } class MyChild : MyBase { public string FirstName {get;set;} public string LastName {get;set;} }

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

          I think another way to phrase this question is How about a way to override the set; method for a property In VS2008 you don't need to specify a private field for properties and can just use public int myprop {get;set;} So if I could figure out how to override that set method, maybe thats where I could add change event handler.

          1 Reply Last reply
          0
          • D dwolver

            Not quite where I was heading, My baseclass will have no idea what properties the child class has so in pseduo code I'm wondering if something like this is possible The idea is to have a means of setting BaseClass protected void SetupHandlers() foreach property in MyProperties AttachSetChangeEvent () public BaseClass() { SetupHandlers(); }

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

            I don't think it's possible. Maybe one of the other guys knows a way - if there is one it could be very interesting.

            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
            • D dwolver

              Not quite where I was heading, My baseclass will have no idea what properties the child class has so in pseduo code I'm wondering if something like this is possible The idea is to have a means of setting BaseClass protected void SetupHandlers() foreach property in MyProperties AttachSetChangeEvent () public BaseClass() { SetupHandlers(); }

              S Offline
              S Offline
              S Senthil Kumar
              wrote on last edited by
              #6

              Not without a lot of work, I'm afraid. With a class like

              class NotifiableProperty<T>
              {
                  T val;
                  public event Action PropertyChanged;
              
                  public T Value
                  {
                      get { return val; }
                      set { val = value; PropertyChanged(); }
                  }
              
                  public static implicit operator T(NotifiableProperty<T> p)
                  {
                      return p.Value;
                  }
              }
              

              you can do

              class MyClass
              {
              NotifiableProperty<int> p = new NotifiableProperty<int>();

                  public MyClass()
                  {
                      p.PropertyChanged += new Action(() => Console.WriteLine("Changed"));
                  }
              
                  public int P
                  {
                      get { return p; }
                      set { p.Value = value; }
                  }
              }
              

              and then when you do

              MyClass m = new MyClass();
              m.P = 20;

              you will see "Changed" printed on the screen. I don't know how to automatically hook to the events, and I don't think setting up events from the base class is a good idea, as there is the possibility that derived class members wouldn't have been initialized at that point. Maybe use reflection inside the derived class?

              Regards Senthil [MVP - Visual C#] _____________________________ My Home Page |My Blog | My Articles | My Flickr | WinMacro

              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