Solved

General Question

Posted on 1998-11-11
9
131 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Machine not responding during CopyFile() 3 89
delphi prevent click fast 2 189
Convert MS Word document to a PDF file 9 63
Graphics32 under Delphi 10.1 Berlin 2 37
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

12 Experts available now in Live!

Get 1:1 Help Now