VBA inheritence using Implements

dugdugdug

Active Member
Joined
May 11, 2012
Messages
342
I have limited knowledge of classes and from my understanding of inheritence, if I defined a class called generalcar with properties such as wheels, bonnet, colour, etc. then by defining another class, called fastcar, if inheritance exists, one could "inherit" the properties of the class generalcar.

Is my definition of inheritance correct?


I read VBA only has interface inheritance, not implementation inheritance.

What is interface inheritance v implementation inheritance?

Thanks
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I have been struggling with this myself recently.

It basically means that if I have an object, and I create a class that is based on that object, I still have to define all the properties and methods that I want to use for my new class. ie VBA doesn't help much with the inheritance of anything...

You can set a property Implements but to me it does not really help with anything.

But if you design your class, say you want a textbox that only allows number input, then you set a member of the class as a textbox, and then within the class procedures and methods you can refer to the properties of the textbox
Code:
private WithEvents mTB as msforms.TextBox

public Property Set NumberBox(byRef TextBx as MsForms.TextBox)
    set mTB = TextBx
end Property

Public Property Get Value() as Double
   Value = cDbl(mTB.Value)
End Property

Public Property Let Value(byVal dNumb as double)
  mTB.Value = cStr(dNumb)
end Property

Private sub mTB_Change()
' the event gets fired on changes to mTB
... do whatever necessary
End sub
 
Upvote 0
Implements in VBA simply says that your class exposes all the properties/methods/events of a specific interface class. As you say, you still have to write the code for those yourself, but it means that you can declare a variable as the interface class type and then assign any object that implements that interface to it.
Much like you can declare a variable as MSForms.Control and then loop through all the different controls on a form using that variable, even though they are different classes (TextBox, CheckBox, CommandButton etc). With your own classes, if you don't use Implements you would need a separate variable for each class.
Of course, whether or not this is of use will depend on what you are doing. :)
 
Upvote 0

Forum statistics

Threads
1,215,409
Messages
6,124,743
Members
449,186
Latest member
HBryant

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top