Connecting Tech Pros Worldwide Forums | Help | Site Map

How to instantiate an array of delegates?

Sharon
Guest
 
Posts: n/a
#1: Nov 16 '05
I have a delegate definition:
public delegate void SomeEventHandler(object obj);

To make an instance of it it’s easy:
public class myClass {
public event SomeEventHandler handler;
}

But, I need to instantiate an array of this delegate and this size will be
known only at the class construction.
I need:
(1) To declare on the delegates array.
(2) To populate this array during the construction with size depending on a
parameter passed as an argument to the constructor.

How to I do that?
I managed to it, but I could not mark the array delegates as events as I did
when I have a single delegate as shown above.

Any ideas?

--
Regards
Sharon G.

Benoit Vreuninckx
Guest
 
Posts: n/a
#2: Nov 16 '05

re: How to instantiate an array of delegates?


Sharon wrote:[color=blue]
> I have a delegate definition:
> public delegate void SomeEventHandler(object obj);
>
> To make an instance of it it’s easy:
> public class myClass {
> public event SomeEventHandler handler;
> }
>
> But, I need to instantiate an array of this delegate and this size will be
> known only at the class construction.
> I need:
> (1) To declare on the delegates array.
> (2) To populate this array during the construction with size depending on a
> parameter passed as an argument to the constructor.
>
> How to I do that?
> I managed to it, but I could not mark the array delegates as events as I did
> when I have a single delegate as shown above.
>
> Any ideas?
>[/color]

Hi,

events can only be declared at the class level. Anywhere else you just
declare variables of the delegate type. Actually, declaring a delegate
instance with or without the event keyword is more or less the same,
except that in the former case, the compiler defines two handy methods
to add (+=) and remove (-=) listeners to and from the delegate. Without
the event keyword you have to manually *Combine* and *Remove* listeners
to/from the delegate variable. The event keyword also make the events
of a component show up in the designer.
Conclusion, you can't declare an array of events, but you can declare an
array of the delegate type.

hope this helps,
Benoit
Closed Thread