Solved

General Question

Posted on 1998-11-11
9
138 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
[X]
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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

717 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