[fpc-pascal] Difference between initialization and begin in a unit

Benedikt Schindler BeniSchindler at gmx.de
Fri Apr 17 00:04:24 CEST 2009


Unit B should just work if it is named "Programm B"
In a Unit there didn't just exist a begin. (as far as i know my 
programms :) )

The initialization  section is called when the unit is linked the first 
time into the programm by a "uses" class.
so it is called before everithing else is going on, even before the 
normal programm ever did a single step.

The initialization section is good for Units that you write, without 
knowing the programm where it is used in.
So you could initialize some variables or objects, and you don't need 
the programmer of the programm who uses your
Unit. (It is also good if you are the programmer who uses the unit. 
Because everything what is needed for the Unit stays in that Unit.)

------------------------------------------------------------

unit A;

interface

type Tsomething = class(TObject)
   ...
 public
   procedure Hello;
  ....
 end;

var

something : TSomething;
ProgrammDir : string;

implementation

....

initialization
  something := Tsomething.create();
  ProgrammDir := Application.Params[0];
finalization 
  something.free;
end.
------------------------------------------------------------


no everyone who uses the Unit A could just use "something.hello" without creating the object.
(because the object just exists from the beginning.)


Beni

btw: i shouldn't post when i am drunk :)

leledumbo schrieb:
> Might be a stupid question, but it's nowhere mentioned in the documentation
> though allowed. What are the differences between:
>
> unit A;
>
> interface
>
> implementation
>
> initialization
>   <initialization code>
>
> end.
>
> and:
>
> unit B;
>
> interface
>
> implementation
>
> begin
>   <initialization code>
>
> end.
>   




More information about the fpc-pascal mailing list