[Repost: since the previous post (Date: Sun, 7 May 2000 13:15:03 +1200) didn't make
the round-trip to me]
> This method is currently I use. But I am not happy about that. Because I
> want that variable to be logical const, no one should modify it. I want
> compiler automatic tell me it is an error if in my code I try to modify the
> value.
You can create a field in your object as a property and have the Class treated as
a singleton (only one copy of the class ever exists)...
unit X;
interface
const
MyClassDefaultValue = 0;
type
TMyClass = class(TObject)
private
FAValue :Integer;
// Declared as private to indicate intended use as a singleton although
// 'Create' will still be available as public due to its declaration on
TObject.
constructor Create(AVAlue :Integer);
public
property AValue :Integer read FAValue;
end;
// Singleton access function ... This could equally be declared as a public
// class method of TMyClass.
function MyClass :TMyClass;
implementation
var
_MyClass :TMyClass;
function MyClass :TMyClass;
begin
// if this is the first invocation then the singleton instance will be created.
if _MyClass=nil then _MyClass := TMyClass.Create(MyClassDefaultValue);
result := _MyClass;
end;
constructor TMyClass.Create(AValue :Integer);
begin
inherited Create;
FAValue := AValue;
end;
initialization
finalization
// Release the Singleton instance if one exists.
_MyClass.Free;
end.
--
Aaron@home
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz