简体   繁体   中英

C# EventHandler returns null when called

I have a two classes.In one class i am creating and raising an event as follows :

CustomerAdd Class

public class CustomerAdd
{
public delegate void Done(object Sender, EventArgs e);
public event Done ListUpdated;

public void UpdateNewList()
{
 //adding items to a generic List<T>,code removed as not relevant to post
 //and raising the event afterwards

 if (ListUpdated != null)
 {
  ListUpdated(this, EventArgs.Empty);
 }
}
}

MyWindow Class

public class MyWindow
{
private void SaveToDisk()
 {
  CustomerAdd cuss = new CustomerAdd();
  cuss.ListUpdated += new CustomerAdd.Done(DisplayDetails);
  cuss.UpdateNewList();
 }
 private void DisplayDetails()
 {
  //other codes here
 }
}

Now, when i call the SaveToDisk method from MyWIndow class,(as i am subscribing DisplayDetails method to the ListUpDated event) , DisplayDetails is not called. The debugger shows that ListUpdated is null. I have searched for hours and failed to come up with a solution.I followed this link but still ListUpdated is null. Any guidance/help would be highly appreciated.

Try this:

using System;

namespace ConsoleApp1
{
    class Program
    {

        static void Main(string[] args)
        {
            CustomerReceive cr = new CustomerReceive();
            cr.SaveToDisk();

        }
    }

    public class CustomerAdd
    {
        public delegate void Done(object Sender, EventArgs e);
        public event Done ListUpdated;

        public void UpdateNewList()
        {
            //adding items to a generic List<T>,code removed as not relevant to post
            //and raising the event afterwards

            if (ListUpdated != null)
            {
                ListUpdated.Invoke(this, EventArgs.Empty);
            }
        }
    }

    public class CustomerReceive
    {
        public void SaveToDisk()
        {
            CustomerAdd cuss = new CustomerAdd();
            cuss.ListUpdated += new CustomerAdd.Done(DisplayDetails);
            cuss.UpdateNewList();
        }
        private void DisplayDetails(object Sender, EventArgs e)
        {
            int k = 0;
        }
    }
}

You need to do a good read on delegates and events because this is not working when there are more listeners

It works:

using System;

namespace ConsoleApp2
{
    class Program
    {

        public class CustomerAdd1
        {
            public delegate void Done(object Sender, EventArgs e);
            public event Done ListUpdated;

            public void UpdateNewList()
            {
                //adding items to a generic List<T>,code removed as not relevant to post
                //and raising the event afterwards

                if (ListUpdated != null)
                {
                    ListUpdated(this, EventArgs.Empty);
                }
            }
        }

        public class CustomerAdd
        {
            public void SaveToDisk()
            {
                CustomerAdd1 cuss = new CustomerAdd1();
                cuss.ListUpdated += new CustomerAdd1.Done(DisplayDetails);
                cuss.UpdateNewList();
            }
            private void DisplayDetails(object Sender, EventArgs e)
            {
                Console.WriteLine("Test");
            }
        }

        static void Main(string[] args)
        {
            var c = new CustomerAdd();
            c.SaveToDisk();
            Console.ReadLine();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM