Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB6 Accessing overloaded interface subroutine of the parent class in the child class

I understand that VB6 does not support inheritance, but it does support interfaces. I am trying to create an overloaded subroutine that passes it's information to the same-named subroutine of the base class.

Sub Main()
    Dim Student1 as New Student
    Dim Teacher1 as New Person

    Teacher1.Init "Mr. Smith"
    Student1.Init "John Doe", Teacher1
End Sub

' --------------------------
' Class definition of Person
Dim name as String

Public Sub Init(n as String)
    name = n
End Sub

' --------------------------
' Class definition of Student
Implements Person

Dim Teacher as Person

Public Sub Init(n as String, t as Person)
    ' I could use the following line to assign the name
    name = n
    ' but I want to be able to call the Init(n) of the parent class

    Set Teacher = t
End Sub
like image 881
David-Hesh Hochhauser Avatar asked Nov 28 '25 13:11

David-Hesh Hochhauser


1 Answers

I'm sorry, but you can't do that in VB6. Nor can you put all of your overloads in the interface, because you'll get an "Ambiguous name detected: Init" error.

You could, however, add your Teacher reference to Person as a self-reference, and when the person is a teacher, just pass the same instance. Like this:

Dim name as String
Dim teacher as Person

Public Sub Init(n as String, t as Person)
    name = n
    teacher = t
End Sub

And then:

Dim Student1 as New Person
Dim Teacher1 as New Person

Teacher1.Init "Mr. Smith", Teacher1
Student1.Init "John Doe", Teacher1   

While it's rather messy (for example, you could call the Init method on the teacher instance that you pass in which is ugly), it's the best you can do in VB6.

like image 178
BobRodes Avatar answered Dec 01 '25 09:12

BobRodes