1
Answer

Understanding Delegates and Events

Ask a question
Rob

Rob

16y
1.9k
1
I'm trying to get down the basics of using delegates and raising events, but the test example I've made doesn't work and I can't for the life of me figure out why.  If anyone would be able to help me out it'd be greatly appreciated, I just want to understand how this aspect of the language works.

Some code:

The test I made consists of an class named Bucket which throws a pair of events requiring a specific kind of delegate.  The second part is a console app which just tries to make an instance of Bucket, attach methods to its events, and then firing those events off.

Bucket:

public class Bucket
    {
        private int ContentsValue;
        private int CapacityValue;
        private string NameValue;
       
        public delegate void VoidEmptyEventDelegate();
        public event VoidEmptyEventDelegate Full;
       
        public delegate void VoidObjectEventDelegate(object sender);
        public event VoidObjectEventDelegate Overflowing;
       
        public Bucket(int capacity)
        {
            CapacityValue = capacity;
            ContentsValue = 0;
        }
       
        public int Contents
        {
            get
            {
                return ContentsValue;
            }
           
            set
            {}
        }
       
        public void Empty()
        {
            ContentsValue = 0;
        }
       
        public string Name
        {
            get
            {
                return NameValue;
            }
           
            set
            {
                NameValue = value;
            }
        }
       
        public void Add(int Amount)
        {
            ContentsValue += Amount;
            if(ContentsValue > CapacityValue)
            {
                Overflowing(this);
            }
            else if(ContentsValue == CapacityValue)
            {
                Full();
            }
        }
    }


Program:

class Program
    {
        public static void Main(string[] args)
        {
            try
            {
                Bucket b = new Bucket(15);
                Bucket.VoidEmptyEventDelegate v = new Bucket.VoidEmptyEventDelegate(BucketIsFull);
                Bucket.VoidObjectEventDelegate vv = new Bucket.VoidObjectEventDelegate(BucketIsOverflowing);

                b.Full += BucketIsFull;
                b.Overflowing += BucketIsOverflowing;
               
                String input;
                input = Console.ReadLine();
                int inputValue;
                inputValue = int.Parse(input);
                b.Add(inputValue);
            }
            catch(Exception x)
            {
                Console.WriteLine("There was an error: {0}", x.Message);
            }
            Console.Write("Press any key to continue . . . ");
            Console.ReadKey(true);
        }
       
        public void BucketIsFull()
        {
            Console.WriteLine("this bucket is full!");
        }
       
        public void BucketIsOverflowing(object sender)
        {
            Console.WriteLine("this bucket is overflowing!");
        }
    }


I get an error when I try to create the Bucket's delegate types within the Program console app.  It's really unintuitive how this is wrong.  Note that the problem goes away if I define the methods I attach to the events as static.  It's not what I want (I don't think so at least) and it's really confusing that that would make the difference.

Any help would be greatly appreciated,
       Rob

Answers (1)