?
Solved

General Question

Posted on 1998-11-11
9
Medium Priority
?
139 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

771 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