Board index » delphi » Having a problem with property overrides not functioning

Having a problem with property overrides not functioning

D3.0 Pro build 5.83, NTW4sp3,

i have a base type declared as follows, to implement an 'abstract property'.

   TScAlignment = Class(TScPoint)
      function  GetLimit : Double; Virtual; Abstract;
      procedure SetLimit( Limit : Double ); Virtual; Abstract;
      procedure Reduce( var Position : TAlignmentPosition ); Virtual; Abstract;
      property BegLimit : Double Read GetLimit;
      property EndLimit : Double Read GetLimit;
      end;

I then have a derived class:

   TBLPoint = Class( TScAlignment )
      U, V, SRad, SAzm, BegSta, PTSt, BegL, EndL : TCoordinate;
      procedure   Load( var fp : text );                         Override;
      procedure   Save( var tf : Text );                         Override;
    [snip]
      property    BegLimit Read BegL Write BegL;
      property    EndLimit Read EndL Write EndL;
      procedure   Reduce( var Position : TAlignmentPosition ); Override;
      end;

TBLPoint declares BegLimit and EndLimit properties as 'overrides' because there is no 'property interface' (ie Double) in the declaration.

From the help:
---------------------
A property definition that doesn't include a property interface is called a property override. A property override allows a derived class to change the visibility, access specifiers, and storage specifiers of an inherited property.
In its simplest form, a property override specifies only the reserved word property followed by an inherited property identifier. This form is used to change the visibility of a property. If, for example, a base class defines a property in a protected
section, a derived class can raise the visibility of the property by declaring a property override in a public or published section.

A property override can include a read, write, stored, and default or nodefault specifier. Any such specifier overrides the corresponding inherited specifier. Note that a property override can change an inherited access specifier or add a missing access
specifier, but it can't remove an access specifier.
---------------------

When i reference BLPoint.BegLimit however, i am greeted with a friendly Abstract Error message.

I was assuming some 'virtuality' inherent in this, am i wrong?  On a close read, its not mentioned.  Of course if there is no virtual aspect whats the value of the different syntaxes (syntactii?) for including/omiting the property interface.

Any ideas?

TIA,

Jay

 

Re:Having a problem with property overrides not functioning


Hi Jay,

I will attempt to answer your questions and I believe for the most part
I'm correct.  However, as in all things my understanding may not be 100%
accurate.

First, when a method is defined as abstract it is assumed that a child
descendent will define the method in its implementation.

Second, the word "override" used in the Delphi help to explain property
overrides I think was a poor choice of words.  There really is no
overriding of the read and write areas of the properties only the
property name.  In other words, the property name can be moved from
protected to publish, etc.  The methods or variables themselves are not
overridden.  That is not to say that the methods used for a property
cannot be overriden.  If the methods are defined as virtual and a child
class wishes to modify these read and write methods then they must
override the methods not the property itself to modify its behaviour.
You can however define new methods for the read and write parts of a
property but is not overriden in the object oriented sense of the word.
 In other words, there is no polymorphism with that approach.  You are
simply defining a new way for the new class or child class to read and
write the property.  The ancestor class will still use its methods
and variables defined for the property.

Third, my guess to the abstract error is simply caused because based on
the code shown there is never an implementation of the abstract methods.
Or the ancestor is in someway referencing the property and it has no
methods defined for it to use.

My suggestion to you is to override the methods defined in the ancestor
class to change the read and write of the property in the child.  Thus,
your child will override the methods defined in the Ancestor and give
its own implementation of the methods.

Note it is still possible to reference the inherited property defined in
the ancestor by using the "inherited" word:

TAncestorObject = class(TObject)
private
  FMyString: String;
protected
  property MyString: String read FMyString write FMyString;
end;

TChildObject = class(TAncestorObject)
protected
  function GetMyString: String;
  procedure SetMyString(Value : String);
public
  property MyString: String read GetMySTring write SetMyString;
end;

****
function TChildObject.GetMyString: string;
begin
  result := inherited MyString;
end;

procedure TChildObject.SetMyString(Value : String);
begin
  inherited MyString := Value;
end;
****

The above really accomplishes nothing but it gives you an idea how to
access an inherited property.  The followin illustrates overriding
methods of a property:

TAncestorObject = class(TObject)
private
  FMyString: String;
protected
  function GetMyString: String; virtual;
  procedure SetMyString(Value : String); virtual;
  property MyString: String read FMyString write FMyString;
end;

TChildObject = class(TAncestorObject)
protected
  function GetMyString: String;
  procedure SetMyString(Value : String);
public
  property MyString: String read GetMySTring write SetMyString;
end;

****
function TAncestorObject.GetMyString: string;
begin
  result := FMyString;
end;

procedure TAncestorObject.SetMyString(Value : String);
begin
  if Value <> FMyString then
    FMyString := Value;
end;

function TChildObject.GetMyString: string;
begin
  result := inherited GetMyString;
end;

procedure TChildObject.SetMyString(Value : String);
begin
  if Value <> 'Good Day' then
    inherited SetMyString(Value);
end;
****

Once again, the above doesn't accomplish much but it can give you an
idea how to implement true polymorphism or "overriding" of your
properties.

I hope this helps,
--
Earl Reddell
Res-cue(Resourceful Components for User Ease!)
Check out our web site: http://www.res-cue.com

--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---

Re:Having a problem with property overrides not functioning


Sorry Jay,

The second example in my previous post for the TChildObject should not
redefine the property but simply declare it as public.

TChildObject = class(TAncestorObject)
protected
  function GetMyString: String;
  procedure SetMyString(Value : String);
public
  property MyString;
end;

---
Earl Reddell
Res-cue(Resourceful Components for User Ease!)
Check out our web site: http://www.res-cue.com

--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---

Other Threads