How to instantiate an array of delegates?

G

Guest

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?
 
B

Benoit Vreuninckx

Sharon said:
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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top