Hi all,
maybe it's a dumb question, but I can't figure out the answer by looking in the archive.
I have a main program which defines and initialises some variables (double, integer, string, arrays). It uses a series of units. I want the units to share the same, initialised variables. For instance, if the variable "rho" is set to 0.01 in the main program, the unit "force" must have the same 0.01 value for the variable "rho".
Thanks to all, best regards
Silvio a Beccara
Hi!
On Thu, Jan 02, 2003 at 09:55:05AM +0100, Silvio a Beccara wrote:
I want the units to share the same, initialised variables.
Just use a unit, where all common variables are defined.
Example:
{ B1: Unit, where rho is defined } unit B1;
interface
var Rho: Real;
implementation
to begin do begin Rho := 0.01 end;
end.
{ B2: Unit uses Rho } unit b2;
interface
procedure UseRho;
implementation
uses b1;
procedure UseRho; begin WriteLn ('Rho is in B2: ', Rho : 0 : 3) end;
end.
{ "Main"-Program Foo } program Foo;
uses b1, b2;
begin WriteLn (Rho : 0 : 3); UseRho end.
Please note, that global variables are a place of hard to find bugs (e.g. local variables might shadow global variables).
It is better to use routines having parameters:
function Calculate (MyRho: Real): Real; begin { do some calculations } { ... } WriteLn ('MyRho is: ', MyRho : 0 : 3); Calculate := { ... } Sin (MyRho) end;
Eike