aehimself 396 Posted April 29, 2021 Hello, I'm wondering how it is possible to achieve. Let's say I have an object: TMyObject = Class(TObject) strict private _status: String; strict protected Property Status: String Read _status Write _status; End; All fine, but how can I publish the same, Status property as public, read-only? In theory I could do (untested, only theory)... TMyObject2 = Class(TMyObject) strict private Function GetStatus: String; public Property Status: String Read GetStatus; End; Function TMyObject2.GetStatus: String; Begin Result := inherited Status; End; and from within TMyObject2's descendants I could say inherited Status := 'MyStatus'; to set the status. Now, can it be achieved that from within the thread I simply say Status := 'MyStatus' and everyone else can access this as a read-only property from the outside? Share this post Link to post
KodeZwerg 54 Posted April 29, 2021 (edited) Why not dont publish "Read" "Write" at all and access internal the used Variable? Edited April 29, 2021 by KodeZwerg Share this post Link to post
Fr0sT.Brutal 900 Posted April 29, 2021 protected property InternalStatus read _status write _status public property Status read _status ? Share this post Link to post
aehimself 396 Posted April 29, 2021 1 minute ago, Fr0sT.Brutal said: protected property InternalStatus read _status write _status public property Status read _status ? This is exactly how I made my workaround. I'm just curious if it's possible to use the same name. Share this post Link to post
Attila Kovacs 629 Posted April 29, 2021 is this some kind of code obfuscation? 😉 Share this post Link to post
aehimself 396 Posted April 29, 2021 1 minute ago, Attila Kovacs said: is this some kind of code obfuscation? 😉 If it would be, it would be a pretty weak attempt, wouldn't it? 😉 Share this post Link to post
Attila Kovacs 629 Posted April 29, 2021 you will tell us the next time you debugging this code 😛 Share this post Link to post
KodeZwerg 54 Posted April 29, 2021 (edited) 3 hours ago, aehimself said: TMyObject = Class(TObject) strict private _status: String; public Property Status: String Read _status; End; that i mean, and within your class-code just adjust "_status" to whatever you like. or implement and publish a private setter to adjust only by code not by property if you know what i mean. TMyObject = Class(TObject) strict private _status: String; private procedure mysetter(const AValue: string); public Property Status: String Read _status; End; Edited April 29, 2021 by KodeZwerg Share this post Link to post
aehimself 396 Posted April 29, 2021 I'm aware of workarounds. I'm interested if the original one can be achieved. Share this post Link to post
KodeZwerg 54 Posted April 30, 2021 10 hours ago, aehimself said: I'm aware of workarounds. I'm interested if the original one can be achieved. Equal how you try, it end in a workaround since you publish it with a "write" property. Share this post Link to post
Fr0sT.Brutal 900 Posted April 30, 2021 22 hours ago, aehimself said: Now, can it be achieved that from within the thread I simply say Status := 'MyStatus' and everyone else can access this as a read-only property from the outside? I didn't investigated deeply but that code in your post seems already doing what you want type TParent = class private FFoo: byte; strict protected property Foo: byte read FFoo write FFoo; end; TChild = class(TParent) property Foo: byte read FFoo ; end; procedure TForm1.Button2Click(Sender: TObject); var par: TParent; ch: TChild; begin par := TParent.Create; par.Foo := 1; // err ch := TChild.Create; ch.Foo := 1; // err end; Share this post Link to post