locked
How to create a custom EVENT in a class in C#.

    Question

  • Using - Visual Studio 2005, C# Forms App

    Problem - 
     There is a user defined property CurrentState in  a class.
     I want to write an event and an event handler in the class. The event handler should be called each time the property CurrentState changes.
    How can I?
    Please help.
    Vibhor Agarwal
    Thursday, November 12, 2009 7:58 PM

Answers

  • Hi,

    Here is a sample code for you. Hope this helps.

    enum State
        {
            StateA,
            StateB,
            StateC
        }
    
        delegate void PropertyChanged();
        
        public class Foo
        {
            private PropertyChanged propertyChanged;
            State currentState;
            public event PropertyChanged OnPropertyChanged
            {
                add
                {
                    propertyChanged += value;
                }
                remove
                {
                    propertyChanged -= value;
                }
            }
    
            public State CurrentState
            {
                get { return currentState; }
                set
                {                
                    if (currentState != value)
                    {
                        currentState = value;
                        if (propertyChanged != null)
                        {
                            propertyChanged();
                        }
                    }
                }
            }
        }
    Here is an interesting post about Event. http://blogs.msdn.com/cburrows/archive/2008/02/18/field-like-events-considered-harmful.aspx
    Happy programming .net
    • Marked as answer by Vibhor Agarwal Saturday, November 14, 2009 7:09 PM
    Friday, November 13, 2009 6:13 PM

All replies

  • Events (C# Programming Guide)

    How to: Publish Events that Conform to .NET Framework Guidelines (C# ...

    The above links describe the .NET Event programming Model.

    How to: Implement Custom Event Accessors (C# Programming Guide)

    The above link describes custom Event Accessors, which provide the most flexible custom event programming.


    Mark the best replies as answers. "Fooling computers since 1971."
    Thursday, November 12, 2009 8:05 PM
  • Another solution might be found in this post on my blog:
    http://bloggingabout.net/blogs/rick/archive/2005/03/21/CreatingEventsUsingDelegates.aspx

    Hope this helps
    Mark the best replies as answers - my blog: http://bloggingabout.net/blogs/rick - my company: http://www.mediagarde.nl
    Thursday, November 12, 2009 10:15 PM
  • Hi,

    Here is a sample code for you. Hope this helps.

    enum State
        {
            StateA,
            StateB,
            StateC
        }
    
        delegate void PropertyChanged();
        
        public class Foo
        {
            private PropertyChanged propertyChanged;
            State currentState;
            public event PropertyChanged OnPropertyChanged
            {
                add
                {
                    propertyChanged += value;
                }
                remove
                {
                    propertyChanged -= value;
                }
            }
    
            public State CurrentState
            {
                get { return currentState; }
                set
                {                
                    if (currentState != value)
                    {
                        currentState = value;
                        if (propertyChanged != null)
                        {
                            propertyChanged();
                        }
                    }
                }
            }
        }
    Here is an interesting post about Event. http://blogs.msdn.com/cburrows/archive/2008/02/18/field-like-events-considered-harmful.aspx
    Happy programming .net
    • Marked as answer by Vibhor Agarwal Saturday, November 14, 2009 7:09 PM
    Friday, November 13, 2009 6:13 PM
  • Here's a common way of defining an event, and firing it.


        class EventDemo
        {
            public event EventHandler<EventArgs> DemoEvent;

            protected virtual void OnDemoEvent(EventArgs e)
            {
                EventHandler<EventArgs> threadSafe = DemoEvent;
                if (threadSafe != null)
                {
                    threadSafe.Invoke(this, e);
                }
            }

            // calling this method perfoms some action that causes event to fire
            public void SomeMethod()
            {
                this.OnDemoEvent(new EventArgs());  // fire the event
            }
        }


    Rudy   =8^D
    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, November 13, 2009 6:32 PM