Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

General Question

Posted on 1998-11-11
9
136 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
ID: 1346535
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
ID: 1346536
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
ID: 1346537
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Author Comment

by:omsec
ID: 1346538
what about that Values that should be initialized only once in a Session, at the program Start...
0
 
LVL 1

Expert Comment

by:AndersWP
ID: 1346539
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
ID: 1346540
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
ID: 1346541
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
ID: 1346542
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
ID: 1346543
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 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