0

I've googled for articles on VB6's support for polymorphism, but all of the articles that I read merely mentioned that you can make a derived class inherit properties and methods from a derived class. None of the three or four articles that I read mentioned whether or not VB6's polymorphism allowed you to pass the derived classes as parameters of the base class type.

If you were to have a class named Bunny, which was derived from a class named Animal, could you pass a variable of type Bunny to the following function?

Public Sub Chase(thePerson as Person, theAnimal as Animal)
    Do While thePerson.position <> theAnimal.position
        ...
    Loop
End Sub
TimFoolery
  • 1,895
  • 1
  • 19
  • 29
  • PS: Bed time for me, so don't get mad if you don't get upvotes and a best answer selection for another 24 hours. – TimFoolery Jul 16 '12 at 06:57
  • VB6 doesn't support derived classes in the usual sense of the term. It doesn't support implementation inheritance. It supports interface inheritance and therefore [polymporphism](http://msdn.microsoft.com/en-us/library/aa242069(v=vs.60).aspx) through the [`Implements` statement](http://msdn.microsoft.com/en-us/library/aa243384(v=vs.60).aspx). Is that what you're asking about? How are you creating your derived class? – MarkJ Jul 16 '12 at 16:00
  • @MarkJ Yes. From the sounds of it, it is basically the same thing as C++ inheritance/polymorphism except (1) the syntax and (2) I'm guessing it's like Java where you can only inherit from one class, but you can implement several; correct me if I'm wrong. I know Java has a similar "extends"/"implements" methodology. – TimFoolery Jul 16 '12 at 20:03
  • You only have the "implements" feature in VB6. You don't have the "extends" / "derives". – MarkJ Jul 16 '12 at 20:35

1 Answers1

0

If your Bunny class Inherits from Animal then yes you should be able to do what you've stated in your sample code.

Raj
  • 1,742
  • 1
  • 12
  • 17
  • -1 because you **cannot inherit** in VB6. http://msdn.microsoft.com/en-us/library/aa260821(v=vs.60).aspx – MarkJ Jul 16 '12 at 20:36
  • @MarkJ Hate to be mean, but I couldn't follow along with that article very well. There are no commas, some sentences simply don't make sense, and it's not well structured. Furthermore, while reading the article, I attempted to write something that featured polymorphism, and it failed. What I am left with are two classes that *contain* an object of type base class, but they are not derived from that base class, so neither of my derived classes can be passed to a function that requires a parameter of the base class type. I'll be posting the code in a new question. Perhaps it's an easy fix. – TimFoolery Jul 17 '12 at 03:05
  • @MarkJ Here's my attempt at following along with the MSDN article: http://stackoverflow.com/questions/11515724/attempting-to-learn-polymorphism-etc-in-vb6-but-my-code-doesnt-do-what-i-wan ; If this is all VB6 is capable of, I am quite disappointed. Surely I am doing something wrong? – TimFoolery Jul 17 '12 at 03:47
  • @Michael I provided the link as evidence that **inheritance is not directly supported** in VB6 and therefore this answer, on which we are commenting here, is **wrong**. Expect to be disappointed. The workarounds are not elegant. Here's [another description](http://www.lhotka.net/Article.aspx?id=5f76a91c-5a75-49e1-9379-6d2807653b68) of the workaround from a long-standing VB6 guru, Rockford Lhotka. – MarkJ Jul 17 '12 at 15:28