I want to create a base class that is meant to be subclassed. That class will raise events that the subclass must deal with, so creating an instance of the super class is useless and I’d like to prevent it. I just can’t figure out how.
I can make the Constructor private, but then the subclass must override it. I can use a Shared Method, but then it must be modified to return the subclass. I could raise a ConstructMe event for which the subclass must return true or an error is raised, but that won’t happen until runtime.
I feel like I’m missing something obvious…
Kem, why make the subclass listen to events?
Kem- make the items that the subclass must implement Methods. In the super, implement the Methods with code that looks like this:
#pragma error "you must override this method in your subclass"
Or something like that. If you want a runtime error, raise an exception.
You could also go the Shared Method route, just make sure that you also make the Constructor Private so users MUST use the shared method. And yes, you’ll need to make the shared method return an instance of the subclass, but it will work.
Why can’t you make the base Constructor protected (not private) and the child class Constructors public? You can still call the base Constructor from the child classes, but no other code can do it.
Thanks for the responses.
Some background that may clear some things up. I want to write a timed document opener class. A very simple thing, App.OpenDocument would use the class’ Append function to add a document that needs to be opened and the class would start an internal Timer to open each document during idle. (This is my preferred way of opening documents since it lets each window form fully before attempting to open the next document.)
The idea is to write the base class with the Append function and the Timer mechanism, then have the Timer raise an OpenThisDocument event that returns a Boolean. If True, the document is removed from the queue, and False would put it back in the queue.
Since I may not use this for weeks or months, or may want to make it publicly available, I would prefer that subclassing be “forced”.
With this in mind, I don’t see how I can use the technique Greg mentioned of forcing the subclass to override a method unless it’s just the Constructor itself since I want the Super to get a message back. (But that’s still a great idea, and one I’ll use elsewhere.) And using the Shared Method to return an instance won’t work since I won’t know anything about the subclass in subsequent projects.
An idea just occurred. Could I use Introspection in the Constructor to see of the instance is more than just the base class? I’ll have to explore that. Otherwise, it seems like forcing an override of the Constructor is the way to go.
Ah okay. The first thing that comes to mind is to use a delegate:
Dim itm as new OpenDocClass(addressOf CallbackMethod)
Wouldn’t that remove the need for subclasses?
Yes, and somewhere in the recesses I considered and dismissed it because it adds a layer of complication I wanted to avoid. (At least, that’s why I dismissed it.)
HOWEVER, now that you bring it up again, it might be the perfect solution. Even if I went with my original scheme, I’d have to use AddHandler to handle the OpenThisDocument event, so why not just use a delegate in the first place?
I’ll check it out and report back. Thanks Greg.
@Kem Tekinay - Just sent you a project off-list.
Got it, thanks. I’m modifying it with a delegate and will send it back for comment.