Jump to content
Sign in to follow this  
Cristian Peța

Hiding a public property in a descendant class

Recommended Posts

TMyEdit = class(TCustomEdit)
private
  property Text;
end;

I try to hide the Text property of TCustomEdit in a descendant class but it doesn't work.

Code insight (LSP) in other unit does not see it but the compiler does not complain if I use it.

Can I do something to hide the Text property?

 

  • Like 1

Share this post


Link to post
TMyEdit = class(TCustomEdit)
private
  function GetText: String;
public
  property Text: String read GetText;
end;

implementation

function TMyEdit.GetText: String;
begin
  Result := TCustomEdit(Self).Text;
end;

It works !!!!

Text property is now read-only.

Do someone knows some side effects of this?

 

There was somewhere a discussion about hiding properties when you re-declare it but I don't remember.

Edited by Cristian Peța

Share this post


Link to post
15 minutes ago, Cristian Peța said:

TMyEdit = class(TCustomEdit)
private
  function GetText: String;
public
  property Text: String read GetText;
end;

implementation

function TMyEdit.GetText: String;
begin
  Result := TCustomEdit(Self).Text;
end;

It works !!!!

Text property is now read-only.

Do someone knows some side effects of this?

You won't have any functionality introduced in TEdit, only the one in TCustomEdit and whatever you implement yourself in TMyEdit. That probably was obvious. 😉

If you declare the property as public, it won't show up in the Object Inspector. For that it needs to be published.

Share this post


Link to post
On 2/18/2021 at 5:48 PM, Cristian Peța said:

Text property is now read-only.

Do someone knows some side effects of this?

(YourEdit as TCustomEdit).Text := 'ahaha'

  • Like 1
  • Thanks 2

Share this post


Link to post
On 2/20/2021 at 9:32 AM, Fr0sT.Brutal said:

(YourEdit as TCustomEdit).Text := 'ahaha'

Ouch! Wtf did they declare it public in TCustomEdit? Most not-yet-published properties of other components are declared protected. But there are exceptions there too: TCustomLabel.Caption is also public. Why?

 

(Yes, I know, I won't get any definite answer here unless some Borland employee talked about it in the 1990s.)

Edited by dummzeuch

Share this post


Link to post
1 hour ago, dummzeuch said:

Ouch! Wtf did they declare it public in TCustmEdit?

Probably because TControl.SetText is implemented via sending WM_SETTEXT so nothing prevents one from sending it directly if he wants to.

Share this post


Link to post
23 hours ago, Fr0sT.Brutal said:

(YourEdit as TCustomEdit).Text := 'ahaha'

Interestingly, but not a problem in this case. I replaced an old component with a new one and I wanted to be sure there is not code that will set the Text property directly.

I don't force it in such a way so no problem.

Share this post


Link to post
On 2/21/2021 at 10:53 AM, Cristian Peța said:

Interestingly, but not a problem in this case. I replaced an old component with a new one and I wanted to be sure there is not code that will set the Text property directly.

I don't force it in such a way so no problem.

Good though there's still a chance you miss some cases

procedure TForm1.EditOnChange(Sender: TObject);
begin
  with TEdit(Sender) do 
    Text := Text + 'lol';
end;

 

  • Thanks 1

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
Sign in to follow this  

×