Hi, I've recently read that you can fake class inheritance by using the "implements" keyword. How can this be done? A very quick example would be helpful. Thanks
Actually the Implements keyword isn't inheritance. What it actually means is to implement an interface. You still have to fill in the code yourself.
Here's an example - all those should be class modules, not standard modules.
The interface named "MyInterface":
Code:
Option Compare Database
Option Explicit
Public Sub DoIt()
End Sub
A class named "ClassA"
Code:
Option Compare Database
Option Explicit
Implements MyInterface
Private Sub MyInterface_DoIt()
MsgBox "This is Class A's implementation"
End Sub
A class named "ClassB"
Code:
Option Compare Database
Option Explicit
Implements MyInterface
Private Sub MyInterface_DoIt()
MsgBox "This is Class B's implementation"
End Sub
It's really more useful when you want two objects to behave similarly but need to write different codes to provide the same behavior. For several reasons, it's not quite easy to find a good practical application of the Implementation, and even if there were, it's severely hampered by various limitations, unfortunately.
Hmm that's what I thought but I got wind you could spoof inheritance with it - forget that then. But is there any other way to simulate inheritance? What's the best option in your opinion?
Well, I just wanted to get a little more info on exactly how to implement inheritance.
My problem, so to say, is that I would like to create a subclass of forms with specialized methods and I would also like to extend the original forms class to include my own additional methods that apply to all forms.
In more detail: many of my forms have been constructed from a single template and have similar functions and hence are running on shared public procedures stored in a module. So, say for an cmdButton_Click event in frmForm1, I simply call the shared sub SharedOnClick.
Code:
Private Sub cmdButton_Click()
SharedOnClick
End Sub
Later, I realise that what I am actually eluding to are classes since these forms are essentially instances of just one class but just with different instance variables. However, Access creates each form as its own class, hence I need an inheritable parent class containing the shared methods (from the module) that are then made available to all aforementioned forms.
To spoof inheritance, in the derived form class I am planning to dim an object called "super" as the parent class and hence be able to access its methods. I will also pass to the parent class the derived form (Me) so that methods in the parent class will be able to operate on the child form and access collections like controls.
Have a look at some cool example of event sinking.
In the end of day, I'm inclined to say that public module is good enough - sure it's nice to get all fancy, but I've found that they just create more work for little gain. I agree it's not pure OOP but that's what it is.