VBA inheritance, super analog

For example, I have class A that implements class B

--- class A ----

implements B public sub B_do() end sub 

- class B ----

 public sub do() end sub 

How can I call do () from A? (super.do ()) So, how can I define some common variable for both classes? Now I can only inherit functions, sub and properties ...

added: the same question http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/5a83d794-3da1-466a-83d3-5d2eb0a054b2

added: it is impossible to pass a variable through a class hierarchy. You must implement a property (just like functions).

+15
inheritance vba excel-vba excel
Sep 08 '10 at 15:22
source share
2 answers

The usual way to do this in VBA is to have A contain an instance of B, and also have an implementation interface B, and then delegate calls to B-interface A to internal B.

This is old stuff, but see the Visual Studio 6.0 Programmer's Guide:

http://msdn.microsoft.com/en-us/library/aa716285(VS.60).aspx

There is a chapter called “Much (Inter) entity code reuse” that describes this convention:

http://msdn.microsoft.com/en-us/library/aa240846(v=VS.60).aspx

The way MS describes this:

In addition to implementing abstract interfaces, you can reuse your implementation code for an interface of a regular class, and then selectively delegating the hidden instance of the class.

This means that implementation inheritance requires many explicit delegation methods. There's even the head of the subtitle: "Isn't that tiring?" Another reason OOP in VBA is PITA (TM) ...

CHANGE THAT DOES NOT TAKE A COMMENT:

To answer the question that you asked in your comment, well, A is B. When you create an implementation interface B, you basically say that you can process an instance of A as if it were actually type B. In VBA, how do you do this by declaring a variable of type B and then setting it to instance A. VBA will know what to do when you name it as B:

 Dim usedAsB as B Dim anA as A Set anA = New A Set usedAsB = anA 'fine since A implements B usedAsB.something() 'will call B_something() defined in class A 

As you can see in the debug window, I don’t understand why it looks like this. And as for the forced delegation, I'm not sure what you mean. VBA automatically sends calls to interface B to the correct methods in class A. If you mean automatically generating code to inherit the implementation of B in the manner described above, I do not know anything similar for VBA. I think various “professional” versions of VB6 can do this, but I never used VB6, so I don’t know.

+21
Sep 08 '10 at 20:01
source share

You can play a trick to imitate inheritance. It works using the default element property.

If you give your derived class a property called Super, whose type is a superclass, then make it the default item (by exporting and editing the file to include Attribute Item.VB_UserMemId = 0 , re-importing), then you can only reach the superclass with a pair of parentheses (which allows the default member).

This blog entry provides complete information, but the author (disclosure, I) uses "Base" instead of "Super"

Hope the syntax is tough enough for you.

I also note that this does not reveal all the internal forces of the base class, as in C #. This means that my method does not suffer from the fragile base class problem. Those. my method saves encapsulation, which makes it better IMHO.

0
Dec 11 '18 at 13:20
source share



All Articles