You may not need a singleton, but how about extracting the property out:
TTest = class(TObject)
private
FValue: Integer;
constructor Create (AValue: Integer);
oublic
property Value: Integer read FValue;
end;
This way, Value is always readonly, and FValue is only accessible from
within the class. The alternative is to create a method:
function GetValue: Integer; // Not very Delphi like
In any case,
type
TTest = object
public
AValue: Integer;
end;
implementation
const
TTest_Null: TTest
= (AValue: 0);
Does not create an object that is readonly. It is merely a form of
initialization for the object. You can still assign to it - because you can
assign to typed constants.
Regards,
Dennis.
----------------------------------------
to
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On
> Behalf Of leigh
> Sent: Sunday, 7 May 2000 17:26
> To: Multiple recipients of list delphi
> Subject: Re: [DUG]: Declare const object
>
>
> Dear Aaron,
>
> Please suggest me if I am wrong.
>
> Singleton is not suitable to me. I got several const objects
> of same class.
>
> Best Regards
> leigh
>
>
> ----- Original Message -----
> From: Aaron Scott-Boddendijk <[EMAIL PROTECTED]>
> To: Multiple recipients of list delphi <[EMAIL PROTECTED]>
> Sent: Sunday, May 07, 2000 1:15 PM
> Subject: Re: [DUG]: Declare const object
>
>
> > > 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.
---------------------------------------------------------------------------
New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
Website: http://www.delphi.org.nz