Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my "Event" always null?

Tags:

c#

I am trying to wire up a new event, but for some reason "Changed" is always evaluating to null

    public class MyTreeViewItem : INotifyPropertyChanged

{
        private MyTreeViewItem _parent;

        public MyTreeViewItem(MyTreeViewItem parent)
        {
            _parent = parent;
        }

        private bool _checked;
        public bool Checked
        {
            get
            {
                return _checked;
            }
            set
            {
                if (value != _checked)
                {
                    _checked = value;
                    OnChanged("test");
                    OnPropertyChanged("Checked");
                }
            }
        }

        public event EventHandler Changed;

        public ObservableCollection<MyTreeViewItem> Children { get; set; }

    // Invoke the Changed event; called whenever list changes
    protected virtual void OnChanged(string test)
    {
        if (Changed != null)
            Changed(this, null);
    }

Subscribing code (PropertyChanged Works, Changed does not)

_playgroupTree = new MyTreeViewItem(null);
AddChildNodes(4, ref _playgroupTree);
_playgroupTree.Changed += new EventHandler(_playgroupTree_Changed);
_playgroupTree.PropertyChanged += new PropertyChangedEventHandler(_playgroupTree_PropertyChanged);

Is really weird because I am also implementing INotifyPropertyChanged (which works), and this code is almost exactly the same (I have tried using the same deligate type, but still it does not work.

I have been using this website as a reference http://msdn.microsoft.com/en-us/library/aa645739%28VS.71%29.aspx

like image 385
Grayson Mitchell Avatar asked Aug 16 '10 21:08

Grayson Mitchell


People also ask

Can events be null?

You're firing the event in the constructor, but you're not adding an event handler to the event until after the object is constructed. Since you haven't yet added any event handlers at the time you fire the event, the event is null.

What is event function in C#?

C# - Events. An event is a notification sent by an object to signal the occurrence of an action. Events in . NET follow the observer design pattern. The class who raises events is called Publisher, and the class who receives the notification is called Subscriber.

What are events in C# with example?

Events are user actions such as key press, clicks, mouse movements, etc., or some occurrence such as system generated notifications. Applications need to respond to events when they occur. For example, interrupts.


2 Answers

Well, you haven't shown any code subscribing to the event. Where do you have code like this:

YourClass yc = new YourClass();
yc.Changed += SomeHandler;

? If you can produce a short but complete program demonstrating the problem, it will be a lot easier to diagnose.

like image 177
Jon Skeet Avatar answered Sep 30 '22 19:09

Jon Skeet


You must attach an event handler to the Changed event, only then will it not evaluate to null.

Changed += (s, e) => Console.WriteLine("received Changed event");
if (Changed != null) Console.WriteLine("now Changed is not null");
like image 44
anthony Avatar answered Sep 30 '22 20:09

anthony