Use the New operator with a class by reference?

Is there a way to use the New operator with a variable class name?

Consider the following class hierarchy:

  • Fruit
  • ---- Orange
  • ---- Apple
  • ---- Grape
  • ---- etc…

Now, a random subclass of Fruit is created and stored in a Fruit variable.

In a method in the Fruit class, we need to make more of the same class. Something like this:

[code]function MakeAnotherFruitJustLikeThisOne(myFruit as Fruit)

dim newFruit as Fruit
newFruit = New [i]<SubclassOf_myFruit>[/i]



And here is my problem. In the Fruit class, when I’m given a random Fruit object that will actually be some subclass of Fruit, I need to make more of the same object. Yes, I could have a switch statement that does a bunch of IsA and figures out which Fruit subclass I’ve actually been given, but I’m looking for a method that is more introspection based. I’d prefer the language to be able to figure this out without having to maintain a manual list of the possible subclasses in a big switch/IsA statement.

Any ideas?

Create a NewInstance method and override it in each subclass. Better, have it raise a GetNewInstance event in each subclass. In the event, you return a new instance of that subclass. Your code would only have to do

dim newFruit as Fruit = oldFruit.NewInstance()

use instropection to get type of the object you have, look for it’s constructor and call it to make new one?

And if there are several constructors then which one do you invoke ?

Introspection gets used for all kinds of things where its really not the right answer