By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,606 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,606 IT Pros & Developers. It's quick & easy.

Event published by array member?

P: n/a
Is there a way that a class can subscribe to an event published by an object
that is a member of an array?

I have a class, Widget, that maintains information on a widget. I have a
number of widgets, so I maintain an array of type Widget[]. Whenever a
widget is changed, the Widget class invokes a Changed event. The Widget[]
array is a property of the WidgetHolder class, and that class needs to know
when the current Widget is changed.

Publishing and invoking the event is no problem. But how does WidgetHolder
subscribe to the event? I can't subscribe when I initialize WidgetHolder,
because I can only get to array properties when I access my Widget[]
property object. To get to individual widgets, I'd have to access Widget[i],
where i is an index number.

It looks like my choices are to use a collection class to wrap the array,
subscribe to the Change event in every member of Widget[] individually, or
pass a reference to WidgetHolder to each Widget as it is created. Is that
right, or am I missing something? Thanks in advance

David Veeneman
Foresight Systems
Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
David Veeneman <da****@nospam.com> wrote:
Is there a way that a class can subscribe to an event published by an object
that is a member of an array?

I have a class, Widget, that maintains information on a widget. I have a
number of widgets, so I maintain an array of type Widget[]. Whenever a
widget is changed, the Widget class invokes a Changed event. The Widget[]
array is a property of the WidgetHolder class, and that class needs to know
when the current Widget is changed.

Publishing and invoking the event is no problem. But how does WidgetHolder
subscribe to the event? I can't subscribe when I initialize WidgetHolder,
because I can only get to array properties when I access my Widget[]
property object. To get to individual widgets, I'd have to access Widget[i],
where i is an index number.

It looks like my choices are to use a collection class to wrap the array,
subscribe to the Change event in every member of Widget[] individually, or
pass a reference to WidgetHolder to each Widget as it is created. Is that
right, or am I missing something? Thanks in advance


No, I think you've got the choices right. I suspect that wrapping the
array is the best bet, personally.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

P: n/a
David,

A few notes which might help you:
1) Why do you need a widgetholder ? Holding widgets ?
Widgets can hold themselves! What does your widget do besides
knowing about themselves ? If nothing remove the widget class too.
2) If your widget is something like a control and your widgetholder is
something like a composite widget then passing the parent to each
widget is a nice choice( look at Control's parent).

Rick


On Sun, 8 May 2005 15:54:30 -0500, "David Veeneman"
<da****@nospam.com> wrote:
Is there a way that a class can subscribe to an event published by an object
that is a member of an array?

I have a class, Widget, that maintains information on a widget. I have a
number of widgets, so I maintain an array of type Widget[]. Whenever a
widget is changed, the Widget class invokes a Changed event. The Widget[]
array is a property of the WidgetHolder class, and that class needs to know
when the current Widget is changed.

Publishing and invoking the event is no problem. But how does WidgetHolder
subscribe to the event? I can't subscribe when I initialize WidgetHolder,
because I can only get to array properties when I access my Widget[]
property object. To get to individual widgets, I'd have to access Widget[i],
where i is an index number.

It looks like my choices are to use a collection class to wrap the array,
subscribe to the Change event in every member of Widget[] individually, or
pass a reference to WidgetHolder to each Widget as it is created. Is that
right, or am I missing something? Thanks in advance

David Veeneman
Foresight Systems


Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.