Jump to content
aehimself

Read-write protected, but read-only public property?

Recommended Posts

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
Posted (edited)

Why not dont publish "Read" "Write" at all and access internal the used Variable?

Edited by KodeZwerg

Share this post


Link to post

protected

  property InternalStatus read _status write _status

public

  property Status read _status

 

?

Share this post


Link to post
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

 

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
Posted (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 by KodeZwerg

Share this post


Link to post

I'm aware of workarounds. I'm interested if the original one can be achieved.

Share this post


Link to post
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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×