I have a form that has a button which creates a new object and calls it’s start() method.
The program works fine, however, I now want to create a stop button. I obviously cannot call the object’s stop() method as it is elsewhere, but, I just can’t think of the correct way of changing my code.
As I write this, the best thing I can think of is to take the MyObject myo = new MyObject("test"); and place MyObject myo; at the top of the class, outside methods and then try to set it from within the class.
What would you do in this situation?
It’s all dependent on scope.
If you want the form, at any time, to have visibility to that object, placing it as a private/protected member within the form’s object is probably a good route. (make sure it’s not null though.
If this object is constantly referenced, you could follow a singleton pattern.
If this is something you can re-create based on [an/the] argument(s) passed to the construct, you can re-create it every time it’s needed.