Board index » delphi » class variables and static variables

class variables and static variables

hi,

is there a way to define a class variable in delphi?

is there a way to define a static variable (a variable inside a
function/procedure which retains its value after it has ended)

please email me the answer directly (itt...@nice.com)

thanks,
ittay

 

Re:class variables and static variables


Hi,

Quote
ittay wrote in message <01bd638f$cdf376c0$d5c8a8c0@ittay>...
>hi,

>is there a way to define a class variable in delphi?

type
  TSomething = class;
  TSomethingDescendant = class(TSomething);

  // class of class type
  TSomethingClass = class of TSomething;

procedure CreateSomething(SomethingClass: TSomethingClass): TSomething
begin
  // from variable class of class you can call constructors, destructors or
class functions
  Result := SomethingClass.Create;
end;

var
  Something: TSomething;
begin
  Something := CreateSomething(TSomethingDescendant);
end;

Quote

>is there a way to define a static variable (a variable inside a
>function/procedure which retains its value after it has ended)

// static variable via typed constant
procedure NextOne: Integer;
const
  Counter: Integer = 0;
begin
  Inc(Counter);
  Result := Counter;
end;

HTH

Martin Schayna

Re:class variables and static variables


Quote
ittay wrote:
> hi,

> is there a way to define a class variable in delphi?

> is there a way to define a static variable (a variable inside a
> function/procedure which retains its value after it has ended)

> please email me the answer directly (itt...@nice.com)

Static Variables within procedures are done as typed constants (note
that this requires the {$J} directive to be set properly:

procedure DoSomething;
const staticVariable:integer=0;
begin
    ...
end;

Class Variables:

There is no true class variable in Delphi, however there are a variety
of ways to emulate them.  Here are four possibilities ordered from
fastest to most elegant:

1) Global variable crude but effective, however not very flexible
because all descendant classes use the same variable.

2) local pointer to global variable allows descendant classes to
override but requires continual dereferencing.  Ex.

TBaseClass = class
private
  FPClassVar:pinteger;
public
  Constructor Create;
  Property PClassVar:pinteger read FClassVar write FPClassVar;
end;

With create setting FPClassVar to a global variable.  Its possible to
hide this global variable as a typed constant within the create
constructor.

Constructor Create;
Const ClassVar:integer=0;
begin
  Inherited Create;
  PClassVar:=@ClassVar;
end;

3) property wrapper around local pointer to global variable looks good
but has that pointer lurking around.

TBaseClass = class
private
  FPClassVar:pinteger;
  Function GetClassVar:integer;
  Procedure SetClassVar(value:integer);
protected
  Property PClassVar:pinteger read FClassVar write FPClassVar;
public
  Constructor Create;
  property ClassVar:pinteger read GetClassVar write SetClass Var;
end;

Function TBaseClass.GetClassVar:integer;
begin
  Result:=PClassVar^;
end;

Procedure TBaseClass.SetClassVar(value:integer);
begin
  PClassVar^:=value;
end;

4) virtualize the pointer inside a function.

TBaseClass = class
private
  function GetClassVar:integer;
  procedure SetClassVar(value:integer);
protected
  function PClassVar:pinteger; virtual;
public
  Constructor Create;
  Property ClassVar:pinteger read GetClassVar write SetClass Var;
end;

function TBaseClass.PClassVar:pinteger;
const ClassVar:integer;
begin
  result:=@ClassVar;
end;

Descendant classes simply override PClassVar to supply their own class
variable;

Bob Lee

Re:class variables and static variables


Quote
ittay wrote:
> hi,

> is there a way to define a class variable in delphi?

> is there a way to define a static variable (a variable inside a
> function/procedure which retains its value after it has ended)

> please email me the answer directly (itt...@nice.com)

Static Variables within procedures are done as typed constants (note
that this requires the {$J} directive to be set properly:

procedure DoSomething;
const staticVariable:integer=0;
begin
    ...
end;

Class Variables:

There is no true class variable in Delphi, however there are a variety
of ways to emulate them.  Here are four possibilities ordered from
fastest to most elegant:

1) Global variable crude but effective, however not very flexible
because all descendant classes use the same variable.

2) local pointer to global variable allows descendant classes to
override but requires continual dereferencing.  Ex.

TBaseClass = class
private
  FPClassVar:pinteger;
public
  Constructor Create;
  Property PClassVar:pinteger read FClassVar write FPClassVar;
end;

With create setting FPClassVar to a global variable.  Its possible to
hide this global variable as a typed constant within the create
constructor.

Constructor Create;
Const ClassVar:integer=0;
begin
  Inherited Create;
  PClassVar:=@ClassVar;
end;

3) property wrapper around local pointer to global variable looks good
but has that pointer lurking around.

TBaseClass = class
private
  FPClassVar:pinteger;
  Function GetClassVar:integer;
  Procedure SetClassVar(value:integer);
protected
  Property PClassVar:pinteger read FClassVar write FPClassVar;
public
  Constructor Create;
  property ClassVar:pinteger read GetClassVar write SetClass Var;
end;

Function TBaseClass.GetClassVar:integer;
begin
  Result:=PClassVar^;
end;

Procedure TBaseClass.SetClassVar(value:integer);
begin
  PClassVar^:=value;
end;

4) virtualize the pointer inside a function.

TBaseClass = class
private
  function GetClassVar:integer;
  procedure SetClassVar(value:integer);
protected
  function PClassVar:pinteger; virtual;
public
  Constructor Create;
  Property ClassVar:pinteger read GetClassVar write SetClass Var;
end;

function TBaseClass.PClassVar:pinteger;
const ClassVar:integer;
begin
  result:=@ClassVar;
end;

Descendant classes simply override PClassVar to supply their own class
variable;

Bob Lee

Re:class variables and static variables


Quote
ittay wrote:
> hi,

> is there a way to define a class variable in delphi?

> is there a way to define a static variable (a variable inside a
> function/procedure which retains its value after it has ended)

> please email me the answer directly (itt...@nice.com)

Static Variables within procedures are done as typed constants (note
that this requires the {$J} directive to be set properly:

procedure DoSomething;
const staticVariable:integer=0;
begin
    ...
end;

Class Variables:

There is no true class variable in Delphi, however there are a variety
of ways to emulate them.  Here are four possibilities ordered from
fastest to most elegant:

1) Global variable crude but effective, however not very flexible
because all descendant classes use the same variable.

2) local pointer to global variable allows descendant classes to
override but requires continual dereferencing.  Ex.

TBaseClass = class
private
  FPClassVar:pinteger;
public
  Constructor Create;
  Property PClassVar:pinteger read FClassVar write FPClassVar;
end;

With create setting FPClassVar to a global variable.  Its possible to
hide this global variable as a typed constant within the create
constructor.

Constructor Create;
Const ClassVar:integer=0;
begin
  Inherited Create;
  PClassVar:=@ClassVar;
end;

3) property wrapper around local pointer to global variable looks good
but has that pointer lurking around.

TBaseClass = class
private
  FPClassVar:pinteger;
  Function GetClassVar:integer;
  Procedure SetClassVar(value:integer);
protected
  Property PClassVar:pinteger read FClassVar write FPClassVar;
public
  Constructor Create;
  property ClassVar:pinteger read GetClassVar write SetClass Var;
end;

Function TBaseClass.GetClassVar:integer;
begin
  Result:=PClassVar^;
end;

Procedure TBaseClass.SetClassVar(value:integer);
begin
  PClassVar^:=value;
end;

4) virtualize the pointer inside a function.

TBaseClass = class
private
  function GetClassVar:integer;
  procedure SetClassVar(value:integer);
protected
  function PClassVar:pinteger; virtual;
public
  Constructor Create;
  Property ClassVar:pinteger read GetClassVar write SetClass Var;
end;

function TBaseClass.PClassVar:pinteger;
const ClassVar:integer;
begin
  result:=@ClassVar;
end;

Descendant classes simply override PClassVar to supply their own class
variable;

Bob Lee

Re:class variables and static variables


Sorry about all the copies, your mail address kept crashing the send.

Bob Lee

Re:class variables and static variables


Quote
"ittay" <itt...@nice.com> wrote:
>hi,

>is there a way to define a class variable in delphi?

>is there a way to define a static variable (a variable inside a
>function/procedure which retains its value after it has ended)

You have already received good answers to both of these.  I would add one
related bit of information.  Variables defined within the UNIT that defines a
component or class act like CLASS variables (shared by all instances etc) but
they are shared throughout all classes defined within the unit (so they are not
really CLASS variables for a specific class).  I use this quite a bit and
define a property with access functions to interface with the UNIT based
storage.  If I need to override it down stream, I create a new UNIT/CLASS
variable and override the access functions.  I've actually used this technique
to handle both situations discussed in the original question.

Nate

 Foolproof systems always underestimate
 the ingenuity of a fool.

 Clipper Functions 4.0 for Delphi
 http://members.aol.com/clipfunc

Other Threads