Solved

General Question

Posted on 1998-11-11
9
130 Views
Last Modified: 2010-04-04
When do you think, is the best Time to initialize general (global) Variables, ie. reading Values from a Config-File or assigning default Values ?

Form1.OnActivate or Form1.OnShow or any better ?
0
Comment
Question by:omsec
  • 2
  • 2
  • 2
  • +2
9 Comments
 
LVL 1

Expert Comment

by:AndersWP
Comment Utility
Well, it would depend on what kind of initialization you are talking about. In my opinion (and remember, this is partly a matter of taste), the following rules should be followed:

Global variables should be initialized in the Initialize section of the unit in which they are declared. They should *not* be initialized in a form object, since you might create more than one instance of the form, thereby having your data initialized more than once.

Variables (fields) in a form object (or any other object), should be initialized in the constructor of the object. Or, for a form, in an OnCreate event handler. Do not use OnShow or OnActivate as these may be called several times, depending on how your form is used.

If you have variables/fields that must be reset every time the form is shown, use OnShow.

I guess that is my 2 cents worth.

Regards,
AndersWP
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
hi omsec,

if you want to intialize you glob-vars once, why not in form.oncreate event?

meikl
0
 
LVL 4

Expert Comment

by:BoRiS
Comment Utility
omsec

This is totally dependent on the way the program works, if you are loading values from a config file then use the OnShow event, the reason I say this is if the user updates the config settings and then hides or minimizes the app.  Then when it's called again to carry on, it will update the the values...when the form is shown again...

But this is totally up to you I think both ways are fine as well as kretzschmar comment

Later
BoRIS
0
 
LVL 2

Author Comment

by:omsec
Comment Utility
what about that Values that should be initialized only once in a Session, at the program Start...
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Expert Comment

by:AndersWP
Comment Utility
omsec:
My opinion is still that values that should initialized only once in a Session, at the program Start, should be set in the Initialization part of the unit that declares them.

Regards,
AndersWP
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
hi omsec,

AndersWP has right. In the Initialization-part you can also set procedures like to read a ini-file. It can alse done by the oncreate event of your masterform.

meikl
0
 
LVL 10

Accepted Solution

by:
viktornet earned 0 total points
Comment Utility
Hello guys!

The default values should be always initialized in the OnCreate() of the form... Except when you need to initialize something about the form's properties like visibility and so on.. If you try initializing properties of the form you will get an Access Violation... otherwise Global variables are initialized in OnCreate() of the form...

Cheers,
Viktor
0
 
LVL 2

Author Comment

by:omsec
Comment Utility
and how does it look at the end of a program ? I mean whats the differenz between Form OnClose and OnDestroy (for examppe, to de-allocate pointers etc.)
0
 
LVL 10

Expert Comment

by:viktornet
Comment Utility
Usually you use OnDestroy() to de-allocate pointers and similar...

As their events' names say it the difference between them is that one executes the code when the form is being Closed and the other is when the Object FORM and all it's childs controls are being destroyed... When you close a form you don't destroy it...you simply hide it.... OnDestroy() happenes when the message FREE is send to it... So you usually use OnDestroy()

Regards,
Viktor Ivanov
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now