Rhino.Kit.AddListener C# (CSharp) Method

AddListener() public static method

Add listener to bag of listeners.
Add listener to bag of listeners. The function does not modify bag and return a new collection containing listener and all listeners from bag. Bag without listeners always represented as the null value.

Usage example:

 private volatile Object changeListeners; public void addMyListener(PropertyChangeListener l) { synchronized (this) { changeListeners = Kit.addListener(changeListeners, l); } } public void removeTextListener(PropertyChangeListener l) { synchronized (this) { changeListeners = Kit.removeListener(changeListeners, l); } } public void fireChangeEvent(Object oldValue, Object newValue) { // Get immune local copy Object listeners = changeListeners; if (listeners != null) { PropertyChangeEvent e = new PropertyChangeEvent( this, "someProperty" oldValue, newValue); for (int i = 0; ; ++i) { Object l = Kit.getListener(listeners, i); if (l == null) break; ((PropertyChangeListener)l).propertyChange(e); } } } 
public static AddListener ( object bag, object listener ) : object
bag object Current collection of listeners.
listener object Listener to add to bag
return object
		public static object AddListener(object bag, object listener)
		{
			if (listener == null)
			{
				throw new ArgumentException();
			}
			if (listener is object[])
			{
				throw new ArgumentException();
			}
			if (bag == null)
			{
				bag = listener;
			}
			else
			{
				if (!(bag is object[]))
				{
					bag = new object[] { bag, listener };
				}
				else
				{
					object[] array = (object[])bag;
					int L = array.Length;
					// bag has at least 2 elements if it is array
					if (L < 2)
					{
						throw new ArgumentException();
					}
					object[] tmp = new object[L + 1];
					System.Array.Copy(array, 0, tmp, 0, L);
					tmp[L] = listener;
					bag = tmp;
				}
			}
			return bag;
		}