Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access a property which is hidden for another property in a child class?

Tags:

oop

delphi

I'm writing a TCustomDBGrid descendent component which need to access the protected property Options(TGridOptions), which is part of the parent class (TCustomGrid) of the TCustomDBGrid object. the issue is which exist a property with the same name reintroduced in the TCustomDBGrid class but with another type (TDBGridOptions).

Check simplified this declaration

 TCustomGrid=  = class(TCustomControl)
 protected
  //I need to access this property
  property Options: TGridOptions read FOptions write SetOptions
      default [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine,
      goRangeSelect];
 end;

  TCustomDBGrid = class(TCustomGrid)
  protected
    //a property with the same name is reintroduced in this class
    property Options: TDBGridOptions read FOptions write SetOptions
      default [dgEditing, dgTitles, dgIndicator, dgColumnResize, dgColLines,
      dgRowLines, dgTabs, dgConfirmDelete, dgCancelOnExit, dgTitleClick, dgTitleHotTrack];   
  end;


  TDBGridEx = class(TCustomDBGrid)
  protected
    //inside of this method I need to access the property TCustomGrid.Options
    procedure FastDraw(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState);
  end;

I figured out how access this property using a cracker class.

type
  TCustomGridClass=class(TCustomGrid);

{ TDBGridEx }
procedure TDBGridEx.FastDraw(ACol, ARow: Integer; ARect: TRect; AState: TGridDrawState);
var
 LOptions: TGridOptions;
 LRect : TRect;
begin
  ......
  LOptions := TCustomGridClass(Self).Options; //works fine
  LRect := ARect;
  if not (goFixedVertLine in LOptions) then
    Inc(LRect.Right);
  if not (goFixedHorzLine in LOptions) then
    Inc(LRect.Bottom);
  .....
end;

But just for curiosity I'm wondering if exist another workaround or a better way for resolve this.

like image 817
RRUZ Avatar asked Aug 25 '12 04:08

RRUZ


1 Answers

Here is another workaround using class helpers. It is not as good hack as yours but works.

type
  TCustomGridHelper = class helper for TCustomGrid
    function GetGridOptions: TGridOptions;
  end;

function TCustomGridHelper.GetGridOptions: TGridOptions;
begin
  Result := Self.Options;
end;

procedure TDBGridEx.FastDraw(ACol, ARow: Integer; ARect: TRect;
  AState: TGridDrawState);
var
 LOptions: TGridOptions;
 LRect : TRect;
begin
  ...
  LOptions := Self.GetGridOptions; //works fine
  ...
end;
like image 174
LU RD Avatar answered Nov 04 '22 13:11

LU RD