Event published by array member?

D

David Veeneman

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,
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
 
J

Jon Skeet [C# MVP]

David Veeneman said:
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,
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.
 
R

Rick Elbers

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
 

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