I have three different classes that I would like to use as singletons in my project. “Easy.”, you say. But hold on. These classes are all subclasses of a parent class. There are multiple methods in that parent class that are used to perform operations since these 3 classes are similar but different. There is only going to be one of each of these classes in the project. Right now I create a new instance of each class type when the app and store them in a global module. Then I call each of those instances when wanting to do something. However, that just seems messy.
But so does creating the singleton as I need to use shared methods. I would need to create shared methods in each subclass that points to the already existing method in the parent class. I thought about making the methods in the parent class shared, but shared methods are shared across ALL instances of a class, so wouldn’t that kind-of muck things up as I have three unique subclasses and now these parent methods which do different things based upon the type of instance - how would I sort that out???
So for example, I have a method in the parent class called “set.” Some of the code is as follows:
If DoChange Then If ControlType <> ControlTypeEnumeration.Sequence Then QueueChanges(IndexToUse, GroupIndex) Else RaiseEvent MakeChange(IndexToUse,DoChange) End If Else ChangeSuccess = True ChangeAction End If
ControlType is an enumeration that defines the type of control the class is representing. It’s different obviously for each subclass. So since the shared method is shared at the parent level between all instances, I won’t get a proper answer for ControlType, will I?
What’s the best way to do this? The way I have it now works and is simple, but using singletons would be convenient.