On Sat, 10 May 2008 10:16:18 -0700, Nathan
<ms********@mac gregorfamily.ne twrote:
Maybe I'm misunderstandin g you, but it sounds like using an interface
would be the easiest solution. Require all your forms to implement an
interface with that method defined; then you can loop through all the
forms and call that method on each.
This is a fine approach as far as it goes. It's certainly a lot closer to
what the OP specifically asked for, and assuming he has the ability to
modify the form classes, is much better than using reflection.
That said, it's a bit "Java-like", especially with respect to defining an
interface with only one method just so you can call that one method. Not
that there's anything technially wrong with being "Java-like" :). Just
that in .NET, with delegates and events, it's more common to approach this
sort of problem using events.
Even .NET has a handful of interfaces that have just one method. It's not
that it's necessarily bad design to do so. But for me, interfaces are
more about abstraction than about conditional processing. With the "is"
or "as" operator, an interface implementation would definitely work fine,
but if one is going to have to change the original class anyway, an event
is IMHO more in line with the usual .NET paradigms than defining an
interface that has only one method. It also avoids the need to enumerate
all of the open forms, instead taking advantage of the multicast nature of
an event to directly retrieve the group of form instances that is exactly
those that will response to the specific operation.
That said, I think that which is preferable may really come down to what
this operation actually is. Since we don't have that detail, only the OP
can determine the answer to that. If it's a case of an operation that is
best thought of as "these forms implement a certain kind of behavior that
needs to be used at some point in time", then I think the interface
approach lends itself very nicely to that. On the other hand, if it's a
case of an operation that is best thought of as "this particular thing
happens at a particular point in time and these forms need to be informed
about that", then an event is probably paradigmaticall y more appropriate.
Pete