[Tutor] Time to Use a Global Variable?

Alan Gauld alan.gauld at freenet.co.uk
Thu Feb 9 10:16:55 CET 2006


Rich,

I love the way you post problems and also post a possible
solution - usually the correct one!  :-)

>   I've tried changing OnFileQuit to call DBinterface.closeDB(), but that
> doesn't work. Obviously I still haven't completely grokked how to refer in
> one class to a method defined in another class. Of course, I've not
> explicitly created an instance of DBinterface within MyFrame, so that may 
> be
> the problem.

Yes that's the problem.

Classes are templates for creating objects, so you don't call methods
on the classes themselves(*). You have to create an instance and work
on that. In your case you would create the instance as part of the
initialisation of the application. You could create the instance outside
the UI and pass it in as an argument to the constructor but either
way the app object has a reference to the database object.

(*)You can define "class methods" which do belong to the class and
can be called directly without an instance but those are rather
esoteric items which we can safely ignore here.

Dipping briefly into analogy mode, this is all quite logical.
When Toyota create a car they do so from a set of designs.
You can't drive the design you have to create an instance of a car
from the design first. The class is like the design and the instance
is the car.

HTH,

Alan G. 



More information about the Tutor mailing list