Sign in Join
Wiki copytemporary

extended class syntax

Static fields, static properties, regular fields and regular properties

pascal
type
  TSomeClass = class
  private
    type
      TSomeType = type integer;    // an internal type
    class var
      FSomeClassVar: TSomeType;    // class variable belongs to a class, not an instance
    var
      FSomeIntanceVar: TSomeType;  // instance variable belongs to instance. it is a usual field
    class procedure SetSomeClassVar(const AValue: TSomeType); static;
  public
    class property SomeProperty: TSomeType read FSomeClassVar write SetSomeClassVar; // class property - belongs to a class
    property SomeInstanceProp: TSomeType read FSomeIntanceVar;
  end;

{ TSomeClass }

class procedure TSomeClass.SetSomeClassVar(const AValue: TSomeType);
begin
   FSomeClassVar := AValue;
end;

var
  SomeClass: TSomeClass;
begin
  SomeClass.SomeProperty := 1;
  WriteLn(SomeClass.SomeProperty);
end.

Class constants

pascal
type
  TSomeClass = class
  strict private
    const
      PrivateConst = 1;
    type
      PrivateType = type Integer;
    var
      FPrivateField: PrivateType;
  public
    procedure DoSomething(Value: PrivateType = PrivateConst);
    function ReturnSomething: PrivateType;
    property SomeProp: PrivateType read FPrivateField write FPrivateField default PrivateConst;
  end;

  procedure TSomeClass.DoSomething(Value: PrivateType = PrivateConst);
  begin
  end;

  function TSomeClass.ReturnSomething: PrivateType;
  begin
    Result := PrivateConst;
  end;

begin
end.

Nested classes

pascal
type
  TSomeClass = class
  strict private
    type
      TPrivateNestedClass = class
      public
        procedure DoSomething;
      end;
  public
    type
      TNestedClass = class
      public
        procedure DoSomething;
      end;
    class procedure Test; virtual;
  end;

  TDescendant = class(TSomeClass)
  public
    class procedure Test; override;
  end;

procedure TSomeClass.TPrivateNestedClass.DoSomething;
begin
  WriteLn('TSomeClass.TPrivateNestedClass.DoSomething: ok');
end;

procedure TSomeClass.TNestedClass.DoSomething;
begin
  WriteLn('TSomeClass.TNestedClass.DoSomething: ok');
end;

class procedure TSomeClass.Test;
var
  P: TPrivateNestedClass;
  N: TNestedClass;
begin
  P := TPrivateNestedClass.Create;
  P.DoSomething;
  P.Free;
  N := TNestedClass.Create;
  N.DoSomething;
  N.Free;
end;

class procedure TDescendant.Test;
var
  N: TNestedClass;
begin
  N := TNestedClass.Create;
  N.DoSomething;
  N.Free;
end;

begin
  TSomeClass.Test;
  TDescendant.Test;
end.

From the Free Pascal wiki, page "extended class syntax", as it stood on 2020-02-14 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 3, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.