[Mono-list] I must misunderstand delegates

Kevin White Kevin White <jedirunner@gmail.com>
Fri, 19 Nov 2004 08:31:16 -0700


Question1:
I'm writing a simple test (shown below) using an event with the
accessor methods add/remove.  I am trying in the remove accessor
method to use Delegate.RemoveAll to get rid of all occurances of
value.  However, RemoveAll takes 2 delegate parameters, removing all
occurances of d2's invocation list from d1's invocation list.  That
doesn't seem to be working when I pass in value as d2.  Also, if I
create a new Delegate with value as its method reference, and passing
this new Delegate instance to RemoveAll, it doesn't remove either. :(

So, how do I change my remove accessor to properly remove all occurances?

Question2:
What do I put in the add accessor to check if value is already in the
delegate's invocation list somewhere, and only add if it is not in the
list?


Here is my source (sorry it's formatted poorly in this gmail.com
compose window):

namespace Test{
	public delegate void AgeChangeHandler(int value);
	public class Person{
		private AgeChangeHandler _ageChange;
		public event AgeChangeHandler AgeChange{
			remove{
//Question1: what do I put here to remove all occurances of value from
_ageChange?
			}
			add{
//Question2: how do I only add to _ageChange if value isn't already in
its invocation list?
			}
		}		

        private int _age;
		public int age{
			get{ return _age; }
			set{
				if(value >= 0 && value <= 200){
					_age=value;
					if (_ageChange != null){
						//invoke the event's delegate
						_ageChange(_age);
					}
				}
			}
		}
	}
}


-- 
Kevin White
jedirunner@gmail.com