Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Inheriting prvate fields


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 4 - Last Post: Dec 5, 2016 12:59 AM Last Post By: Toby Dobbs
Toby Dobbs

Posts: 77
Registered: 9/24/16
Inheriting prvate fields  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 2, 2016 3:43 AM
I have class which descends from TSpinEdit. I am trying to override the Getter and Setter methods for the Value property. In order to do so I would like to access the ancestor's (TSpinEdit) private fields. I understand that private fields, inherently, cannot be accessed from a descendant and should be protected at most. However, I tried to work around this by casting the descendant to be its ancestor as in TSpinEdit(Self).FMinValue but this doesn't seem to work either. How do I get round this issue?
Jeff Overcash (...

Posts: 1,529
Registered: 9/23/99
Re: Inheriting prvate fields  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 2, 2016 8:43 AM   in response to: Toby Dobbs in response to: Toby Dobbs
Toby Dobbs wrote:
I have class which descends from TSpinEdit. I am trying to override the Getter and Setter methods for the Value property. In order to do so I would like to access the ancestor's (TSpinEdit) private fields. I understand that private fields, inherently, cannot be accessed from a descendant and should be protected at most. However, I tried to work around this by casting the descendant to be its ancestor as in TSpinEdit(Self).FMinValue but this doesn't seem to work either. How do I get round this issue?

Which one can't you access? These are the private fields I see

FMinValue: LongInt; -> Directly accessible by MinValue property
FMaxValue: LongInt; -> Directly accessible by MaxValue property
FIncrement: LongInt; -> Directly accessible by Increment property
FButton: TSpinButton; -> Directly accessible by Button property
FEditorEnabled: Boolean; -> Directly accessible by EditorEnabled property

All the private fields of that class have propertys that jsut map directly to
them so have no side effects to using hte property over the direct access to the
field.

--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
Learning is finding out what you already know. Doing is demonstrating that you
know it. Teaching is reminding others that they know it as well as you. We are
all leaners, doers, teachers. (R Bach)
Toby Dobbs

Posts: 77
Registered: 9/24/16
Re: Inheriting prvate fields  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 2, 2016 8:54 AM   in response to: Jeff Overcash (... in response to: Jeff Overcash (...
Jeff Overcash (TeamB) wrote:
Toby Dobbs wrote:
I have class which descends from TSpinEdit. I am trying to override the Getter and Setter methods for the Value property. In order to do so I would like to access the ancestor's (TSpinEdit) private fields. I understand that private fields, inherently, cannot be accessed from a descendant and should be protected at most. However, I tried to work around this by casting the descendant to be its ancestor as in TSpinEdit(Self).FMinValue but this doesn't seem to work either. How do I get round this issue?

Which one can't you access? These are the private fields I see

FMinValue: LongInt; -> Directly accessible by MinValue property
FMaxValue: LongInt; -> Directly accessible by MaxValue property
FIncrement: LongInt; -> Directly accessible by Increment property
FButton: TSpinButton; -> Directly accessible by Button property
FEditorEnabled: Boolean; -> Directly accessible by EditorEnabled property

All the private fields of that class have propertys that jsut map directly to
them so have no side effects to using hte property over the direct access to the
field.

That is one way of doing it yes but I was curious about the workaround method and whther it would work - i'm surprised it doesn't you see.


--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
Learning is finding out what you already know. Doing is demonstrating that you
know it. Teaching is reminding others that they know it as well as you. We are
all leaners, doers, teachers. (R Bach)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Inheriting prvate fields  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 3, 2016 12:55 PM   in response to: Toby Dobbs in response to: Toby Dobbs
Toby wrote:

I have class which descends from TSpinEdit. I am trying to override
the Getter and Setter methods for the Value property.

You cannot, because they are not virtual methods.

However, the Value property setter assigns a new value to the inherited Text
property, so if you want to react to Value changes then you need to handle
the CM_TEXTCHANGED message, eg:

type
  TMySpinEdit = class(TSpinEdit)
  private
    procedure CMTextChanged(var Message: TMessage); message CM_TEXTCHANGED;
  end;
 
procedure TMySpinEdit.CMTextChanged(var Message: TMessage);
begin
  inherited;
  // do something...
end;


Or:

type
  TMySpinEdit = class(TSpinEdit)
  protected
    procedure WndProc(var Message: TMessage); override;
  end;
 
procedure TMySpinEdit.WndProc(var Message: TMessage);
begin
  inherited;
  if Message.Msg = CM_TEXTCHANGED then
  begin
    // do something...
  end;
end;


In order to do so I would like to access the ancestor's (TSpinEdit) private
fields.

You cannot, since they are private and not accessible to descendants. Descendants
can only use public/protected properties and methods.

I understand that private fields, inherently, cannot be accessed from
a descendant and should be protected at most. However, I tried to
work around this by casting the descendant to be its ancestor as in
TSpinEdit(Self).FMinValue but this doesn't seem to work either.

No, it wouldn't, because your derived class is not declared in the same unit
that TSpinEdit is declared in, so your class does not have friendship rights
to access TSpinEdit's (non-strict) private members directly.

How do I get round this issue?

You have to use the public properties instead of the private fields.

--
Remy Lebeau (TeamB)
Toby Dobbs

Posts: 77
Registered: 9/24/16
Re: Inheriting prvate fields  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 5, 2016 12:59 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thanks
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02