What you read only applies to const parameters. I would continue to use
method level consts and not move them to the unit level. In fact, I *never*
use unit level vars, except for the Delphi generated main form code. Using
method level is safer than unit level vars.
Eg. in multi-threading apps, the method can contain synchronisation code to
serialise access to the variable.
You can implement singleton classes like this:
TSingleton = class
function GetInstance: TSingleton;
end;
function TSingleton.GetInstance: TSingleton;
const
FInstance: TSingleton = Nil;
begin
if not Assigned (FInstance) then begin
// Put thread sync code here
FInstance := TSingleton.Create;
end;
Result := FInstance;
end;
Dennis.
----- Original Message -----
From: "Alistair George" <[EMAIL PROTECTED]>
To: "Multiple recipients of list delphi" <[EMAIL PROTECTED]>
Sent: Friday, June 13, 2003 3:49 PM
Subject: [DUG]: Non-global Constants in a function
> In the past I have used const in a proc or func to allow re-entering the
routine
> later to use the same variable value. But reading the following help it
seems I
> am doing the wrong thing. Should I be using a global variable instead?
>
> QUOTE
> A constant (const) parameter is like a local constant or read-only
variable.
> Constant parameters are similar to value parameters, except that you
cant
> assign a value to a constant parameter within the body of a
procedure or
> function, nor can you pass one as a var parameter to another routine.
(But when
> you pass an object reference as a constant parameter, you can still
modify the
> objects properties.)
>
> eg
> function Tmainform.ParseAddFiles(FName: string; Recurse: boolean): string;
> const PrevDIR: string = '
';
> begin
> result := '';
> if pos('*.*', fname) > 0 then
> begin
> PrevDIR := extractfiledir(fname); //This remains until redefined??
> result := Fname;
> exit;
> end;
> if extractfiledir(fname) = prevdir then //this file is already in *.* so
omit
> begin
> result := '';
> exit;
> end;
> end;
>
> --------------------------------------------------------------------------
-
> New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
> Website: http://www.delphi.org.nz
> To UnSub, send email to: [EMAIL PROTECTED]
> with body of "unsubscribe delphi"
> Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/
>
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED]
with body of "unsubscribe delphi"
Web Archive at: http://www.mail-archive.com/delphi%40delphi.org.nz/