Cristian Peța 103 Posted February 18, 2021 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? 1 Share this post Link to post
Bill Meyer 337 Posted February 18, 2021 (edited) Visibility can be increased, but not decreased. I seem to recall someone using a trick to gain the effect, but can't recall when or where. This is one of the drawbacks of inheritance. You may find this discussion helpful: https://stackoverflow.com/questions/4749867/delphi-how-to-hide-ancestor-methods Edited February 18, 2021 by Bill Meyer 1 Share this post Link to post
Cristian Peța 103 Posted February 18, 2021 More I read on this subject I understand this is right. Share this post Link to post
Cristian Peța 103 Posted February 18, 2021 (edited) 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 February 18, 2021 by Cristian Peța Share this post Link to post
dummzeuch 1505 Posted February 18, 2021 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
Fr0sT.Brutal 900 Posted February 20, 2021 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' 1 2 Share this post Link to post
dummzeuch 1505 Posted February 20, 2021 (edited) 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 February 21, 2021 by dummzeuch Share this post Link to post
Fr0sT.Brutal 900 Posted February 20, 2021 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
Cristian Peța 103 Posted February 21, 2021 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
Fr0sT.Brutal 900 Posted February 24, 2021 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; 1 Share this post Link to post