Ohne static würde es funktionieren.
Code: Alles auswählen
type
TTest = class(TObject)
public
const i: integer = 5; static; // wie geht dies ?
end;
Code: Alles auswählen
type
TTest = class(TObject)
public
const i: integer = 5; static; // wie geht dies ?
end;
Was du haben willst, wird wohl eher eine typisierte Konstante sein (also deine funktionierende Variante), die dann aber nicht statisch wäre.http://lazarus-ccr.sourceforge.net/fpcdoc/ref/refse30.html#x66-740006.1 hat geschrieben:Similar to objects, if the {$STATIC ON} directive is active, then a class can contain static fields: these fields are global to the class, and act like global variables, but are known only as part of the class. They can be referenced from within the classes’ methods, but can also be referenced from outside the class by providing the fully qualified name.
For instance, the output of the following program:
{$mode objfpc}
{$static on}
type
cl=class
l : longint;static;
end;
...
Code: Alles auswählen
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
TTest = class(TObject)
private
i: integer;
class var
str1: string;
const
str2: string = '1234';
var
str3: string;
// str4: string = '1234'; // geht nicht
public
constructor Create(s: string);
procedure Ausgabe;
end;
implementation
constructor TTest.Create(s: string);
begin
inherited Create;
str1 := s;
str2 := s;
str3 := s;
end;
procedure TTest.Ausgabe;
begin
Writeln(str1, ' - ', str2, ' - ', str3);
end;
end.
In Pascal ist die einzige vorgesehene Möglichkeit der Construktor. Der wurde extra für solche Aufgaben erfunden.Mathias hat geschrieben:Wie kann ich str4 einen Startwert zuordnen ?
Ich könnte es im Constructor machen, aber wen es anderst geht will ich das nicht.