views:

60

answers:

2

I have a base class where I derive several classes. I have another class that uses all those derived classes in a different way. However, I want to call the Update() method (inherited from the base class) on each derived class. Is there an easy way to do this, or do I have to do something like:

dim a As Derived1
a.Update

dim b As Derived2
b.Update

etc...
+5  A: 

I think the best way to do this is to keep the derived objects in a list of some kind, and then iterate over them to call Update.

In pseudo-code:

foreach BaseClass item in {a, b, ...}:
    item.Update
recursive
+1  A: 

You can do that through polymorphism with a function call that's passed the base class (pseudo-code):

Dim Dev1 as Derived1 '// This is derived from the class BaseClass
Dim Dev2 as Derived2 '// This is derived from the class BaseClass

CallUpdate(Dev1)
CallUpdate(Dev2)

Function CallUpdate(BaseClass bc)
    bc.Update()
End Function
CAbbott
+1 I was just about to post the same answer :)
Kelsey
You're just trading .Update for CallUpdate. What's the benefit?
recursive
I was abstracting the BaseClass functionality through the function call. I read the OP as asking if there was a way to call BaseClass functionality in a common way.
CAbbott
no, i know how to do that, hence stating that the `Update` method is inherited. I want a way to call all the derived classes' `Update` method at once.
Jason
@Jason - then you want recursive's answer. :)
CAbbott