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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With