+ Reply to Thread
Results 1 to 5 of 5

(Best alternative to) Class inheritance in VBA

  1. #1
    Registered User
    Join Date
    01-10-2018
    Location
    Italy
    MS-Off Ver
    Excel 365 (2021-01)
    Posts
    71

    Question (Best alternative to) Class inheritance in VBA

    Fact: class inheritance isn't supported in VBA.

    So, let's assume that you want to add some personal methods/properties to the Selenium webdriver object, in a personal class named ExtendedWebDriver.
    What's your favorite way to do that?

    I currently organize that to have :
    WebDr.o (original Selenium webdriver object, containing all its original methods/properties)
    WebDr.myExampleMethod1
    WebDr.myExampleMethod2

    That is:
    Please Login or Register  to view this content.
    Better ideas?

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: (Best alternative to) Class inheritance in VBA

    I grasp at the conceptual level what you want to do, but I think there are a couple of issues in your code example. Also, you need to decide if you want to truly emulate inheritance, or just get the job done in an easy way.

    First, in your example, Property must be Get, Let, or Set. I think you need this:

    Please Login or Register  to view this content.
    Secondly, allowing the caller to retrieve the original object breaks the abstraction you are trying to create. That can work but exposes to the caller that you have the original object, plus a bunch of stuff you have added. Plus, from the caller's standpoint, there is no guarantee that operating on that object will continue to be reflected in the extended object. To truly replicate inheritance, you would have to code every property and method of the original, and not allow access to the underlying object. Otherwise the caller has to know when it's dealing with your extended class vs. the original class. If you hide the original object, then the caller does not have to go through the extra step of retrieving the original object, and operating on that. However, this requires writing a method and property for every method and property in the original class (which normally inheritance would do for you under the covers).
    Please Login or Register  to view this content.
    Further, as to your example, it is not clear how you can reference a non-existent method or property in the WebDr object:
    Please Login or Register  to view this content.
    I think you would need something like this to create a new method, which references available methods/properties:
    Please Login or Register  to view this content.
    or something like this which creates a new property:
    Please Login or Register  to view this content.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    01-10-2018
    Location
    Italy
    MS-Off Ver
    Excel 365 (2021-01)
    Posts
    71

    Re: (Best alternative to) Class inheritance in VBA

    Wow 6StringJazzer thanks for the enlightening answer!

    Yes sorry my errors that you evidenced in red, you're right and you correctly understood the concept that I wanted to express.

    Now, doing it the way you wrote, is there a way to also check/ensure that I implemented every method and property of the original class?
    I know that "Implements" does that check but with it I would not be allowed to add my personal methods/properties.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,995

    Re: (Best alternative to) Class inheritance in VBA

    I have not used the Implements statement so I would have to study that.

    I don't think there is any way to automatically check that your have implemented every property and method. Let me look into it and see what I can come up with.

    However, the lack of a language mechanism for inheritance in VBA (much less multiple inheritance, or the Interface feature from Java) makes pure OOP very much a rompicoglioni.

  5. #5
    Registered User
    Join Date
    01-10-2018
    Location
    Italy
    MS-Off Ver
    Excel 365 (2021-01)
    Posts
    71

    Re: (Best alternative to) Class inheritance in VBA

    eheheh

    I just saw here https://rubberduckvba.wordpress.com/...ent-interface/
    that Rubberduck has this interesting feature:

    "Implement Interface
    Implementing all members of an interface (and all members of an interface must be implemented) can be tedious; Rubberduck automatically creates a stub method for every member of the interface specified in an Implements statement."

    That, and the other features in that page, seem interesting but I don't know how it works. When I click it, even if an "Implements classname" line is selected, it does nothing.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Property inheritance
    By amphinomos in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-02-2016, 06:38 AM
  2. What would be the best alternative to the common "List" class in VBA?
    By Durarara in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-08-2015, 07:17 AM
  3. If statement help for inheritance
    By notEXCELing in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 06-19-2013, 11:39 PM
  4. Ways to fake inheritance for Class Modules?
    By AdamBecker in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-08-2013, 06:36 PM
  5. ActiveWorkbook Inheritance
    By msleger in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-02-2007, 02:39 PM
  6. [SOLVED] inheritance
    By Bernard Bourée in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 08-18-2006, 01:20 PM
  7. inheritance module
    By @ndy in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-11-2005, 01:06 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1