Search code examples
variablesinheritancevb6member

VB6 Member variable inheritance


I'm having trouble inheriting a (public) variable, let's say

Public Var As ClassThatIsIndependent

The declaration above generates no trouble for itself, however, if i inherit the class that holds it

Implements BaseClass

I get the error "object module needs to implement variable for interface". I've tried these options (both inside ChildClass)

Public Var As ClassThatIsIndependent

and

Public BaseClass_Var As ClassThatIsIndependent

But none of them solves the problem. Any alternative? I'm open to possible Set/Get solutions, however, i'd prefer to maintain Var as a public variable.


Solution

  • Per the Visual Basic 6.0 Programmer's Guide, Polymorphism, Implementing Properties section:

    Suppose we give the Animal class an Age property, by adding a Public variable to the Declarations section:

    Option Explicit
    Public Age As Double
    

    The Procedure drop downs in the code modules for the Tyrannosaur and Flea classes now contain property procedures for implementing the Age property,

    Using a public variable to implement a property is strictly a convenience for the programmer. Behind the scenes, Visual Basic implements the property as a pair of property procedures.

    You must implement both procedures. The property procedures are easily implemented by storing the value in a private data member, as shown here:

    Private mdblAge As Double
    
    Private Property Get Animal_Age() As Double
       Animal_Age = mdblAge
    End Property
    
    Private Property Let Animal_Age(ByVal RHS As Double)
       mdblAge = RHS
    End Property
    

    The private data member is an implementation detail, so you have to add it yourself.

    That is, the "public interface" is exactly the same whether you use a Public variable or define them with Property Get/Let. And to implement a property in an interface, you can't use the Public variable approach and need to use the Property Get/Let syntax and handle the data storage for it in your own private variables.