Trevor Jones wrote:
> 
> >Global variables means non-object-oriented, non-reusable,
> >non-understandable, non-maintainable code. Try building a large project
> >with everything global and find out!
> 
> Your friend and mine, the Delphi IDE automatically creates a global variable
> for each form you design.  If they're that bad, why would the IDE create
> them?  From a simplicity point of view, they can come in very handy.  In the
> case of global form variables, I usually put something like this in my
> Form's OnDestroy event:

Being mildly anti-globals, I've come up with an idea to fix the way Delphi
(and C++ Builder) use Globals to represent the forms of the application. 
Why not have an application built as a descendant of TApplication in the
same way that our forms descend from TForm?  Then each auto-created form in
the application would be a member of TApplication1 (or whatever we decide to
name the descendant class) instead of a global variable.  Then the only
global var we'd need is the Application itself.

<silly>Oh, and of course all the other global objects should be moved into
the appropriate object - Screen into Application1, etc.</silly>

I occaisionally use code similar to some of the things you mentioned in your
post.  In one of our current projects we have several build-on-demand forms
that may never be instantiated if the user doesn't need them, but one
instantiated they're in memory for the remainder of the program's
lifecycle.  It means faster startups, especially as many of them have a fair
number of tables, queries, etc. that have to be connected before it can be
shown, etc.  We cut down the load time of one project from a couple of
minutes to a few seconds (with a good tail-wind to the Database server) this
way.

Taking your first code fragment a step further, I've occaisionally assigned
to the global object from the constructor.  If the object is unassigned then
I assign self to it, otherwise I assume there's been some mistake and leave
it alone.  This is also handy if you want to enfore single-instance objects.

-- 
Corey Murtagh
The Electric Monk
"Quidquid latine dictum sit, altum viditur!"
---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"

Reply via email to