Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After looking into this again, I have an answer, but it's not exactly satisfying. As with most OOP in VBA, it involves using an appropriate interface, but the fact that every class (and every interface) has to go in a separate class module makes it a pretty clunky way to do things. So here goes:</p> <p>Say I have a class called MyClass (which I was calling 'C' above but I'm now calling 'MyClass' to make this clearer.)</p> <p>The thing to do would be to define an interface that I would actually use in my code that exposed just the things about MyClass that I wanted truly public. Say this code is in a class module called IMyClass:</p> <pre><code>Public Function calcSomething() End Function Public Function equal(cinst As IMyClass) As Boolean End Function </code></pre> <p>Then I have my class called MyClass, defined with this code in a class module:</p> <pre><code>Implements IMyClass Private hidden1_ As Double Private hidden2_ As Double Public Sub init(h1 As Double, h2 As Double) hidden1_ = h1 hidden2_ = h2 End Sub Public Function equalDef(hidden1 As Double, hidden2 As Double) As Boolean equalDef = (hidden1_ = hidden1 And hidden2_ = hidden2) End Function Private Function IMyClass_calcSomething() As Variant IMyClass_calcSomething = hidden1_ * hidden2_ End Function Private Function IMyClass_equal(cinst As IMyClass) As Boolean If TypeOf cinst Is MyClass Then Dim asMyClass As MyClass Set asMyClass = cinst IMyClass_equal = asMyClass.equalDef(hidden1_, hidden2_) End If End Function </code></pre> <p>And here is some code that would go in a regular module:</p> <pre><code>Public Function mkMyClass(h1 As Double, h2 As Double) As IMyClass Dim ret As MyClass Set ret = New MyClass Call ret.init(h1, h2) Set mkMyClass = ret End Function Public Sub useMyClass() Dim mc1 As IMyClass Set mc1 = mkMyClass(42, 99) Dim mc2 As IMyClass Set mc2 = mkMyClass(42, 99) Dim mc3 As IMyClass Set mc3 = mkMyClass(99, 42) Debug.Print mc1.calcSomething Debug.Print mc1.equal(mc2) Debug.Print mc3.calcSomething Debug.Print mc3.equal(mc2) End Sub </code></pre> <p>In the 'useMyClass' routine, you can verify that the various mc1, mc2, and mc3 variables can't see the 'init' or 'equalDef' methods of MyClass. They can only see the 'calcSomething' and 'equal' methods that are part of the interface.</p> <p>So, question officially but unsatisfyingly answered, I guess. At least it gives me a chance to repeat "OOP in VBA is a PITA (TM)"...</p> <p>Here are some related stackoverflow answers:</p> <p><a href="https://stackoverflow.com/questions/3669270/vba-inheritance-analog-of-super/3671434#3671434">VBA inheritance, analog of super</a></p> <p><a href="https://stackoverflow.com/questions/1731052/is-there-a-way-to-overload-the-constructor-initialize-procedure-for-a-class-in/1744818#1744818">Is there a way to overload the constructor / initialize procedure for a class in VBA?</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload