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.
>
> --
> Aaron@home
>
>
> --------------------------------------------------------------------------
-
>     New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
>                   Website: http://www.delphi.org.nz
>

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

Reply via email to