12

I need to fix a third-party component. This component's class has private variable which is actively used by its descendants:

TThirdPartyComponentBase = class
private
  FSomeVar: Integer;
public
  ...
end;

TThirdPartyComponent = class (TThirdPartyComponentBase)
protected
   procedure Foo; virtual;
end;

procedure TThirdPartyComponent.Foo;
begin
  FSomeVar := 1; // ACCESSING PRIVATE FIELD!
end; 

This works because both classes are in the same unit, so they're kinda "friends".

But if I'll try to create a new class in a new unit

TMyFixedComponent = class (TThirdPartyComponent)
  procedure Foo; override; 
end;

I can't access FSomeVar anymore, but I need to use it for my fix. And I really don't want to reproduce in my code all that tree of base classes.

Can you advise some quick hack to access that private field without changing the original component's unit if it's possible at all?

4 Answers 4

20

By the use of class helpers it's possible to accomplish access to the private parts of the base class from the derived class without loosing type safety.

Just add these declarations in another unit:

Uses YourThirdPartyComponent;

type
  // A helper to the base class to expose FSomeVar
  TMyBaseHelper = class helper for TThirdPartyComponentBase
  private
    procedure SetSomeVar( value : integer);
    function GetSomeVar: integer;
  public
    property SomeVar:integer read GetSomeVar write SetSomeVar;
  end;

  TMyFixedComponent = class helper for TThirdPartyComponent
  protected
    procedure Foo;
  end;

procedure TMyFixedComponent.Foo;
begin
  // Cast to base class and by the class helper TMyBaseHelper the access is resolved
  TThirdPartyComponentBase(Self).SomeVar := 1; 
end;

function TMyBaseHelper.GetSomeVar: integer;
begin
  Result := Self.FSomeVar; // ACCESSING PRIVATE FIELD!
end;

procedure TMyBaseHelper.SetSomeVar(value: integer);
begin
  Self.FSomeVar := value; // ACCESSING PRIVATE FIELD!
end;

// Testing
var
  TSV: TThirdPartyComponent;
begin
  TSV := TThirdPartyComponent.Create;
  try
    TSV.Foo;    
    WriteLn(IntToStr(TSV.SomeVar));  // Writes 1
  finally
    TSV.Free;
  end;
end.

As can be seen from comments in code, FSomeVar is exposed by a class helper from the TThirdPartyComponentBase class. Another class helper for the TThirdPartyComponent implements the Foo procedure. In there, access to the SomeVar property of the base class helper is made via a type cast to the base class.

2
  • 1
    Very nice! I like it.
    – J...
    Sep 18, 2014 at 14:13
  • 5
    Note that class helpers in another unit can no longer access private members as of Delphi 10.1 Berlin, because this was regarded as a bug by Embarcadero. See stackoverflow.com/questions/9410485/… for more detail Apr 21, 2016 at 5:14
6

You have to use a hack to access a private field in any class (including a base class) in a different unit. In your case define in your unit:

type
  __TThirdPartyComponentBase = class 
  private 
    FSomeVar: Integer;
  end;

Then get the access:

__TThirdPartyComponentBase(Self).FSomeVar := 123;

Of course, that is dangerous, because you will need to control changes in the base class. Because if the fields layout will be changed and you will miss this fact, then the above approach will lead to failures, AV's, etc.

5
  • 2
    @Andrew: note that this solution will break as soon as the memory layout of the ancestor (3rd party) component changes. You might not notice it breaks, as nothing will warn you about it. Or you might see spurious wrong behaviour (if you are lucky: access violations) because you start to overwrite data that is not yours. Oct 28, 2010 at 15:50
  • @Jeroen Pluimers I have notices Andrew about that fact. But there is no other solutions for this issue.
    – oodesigner
    Oct 28, 2010 at 16:14
  • Class helpers can do this without hacking, see my answer :)
    – LU RD
    Jul 7, 2012 at 22:19
  • @LURD Class helpers are only available from Delphi 2007 or 2009. (I'm not sure.)
    – mg30rg
    Sep 19, 2014 at 9:40
  • @mg30rg, class helpers were introduced in .net (D8) and D2005.
    – LU RD
    Sep 19, 2014 at 9:42
0

Don't know if this will help, but I seem to recall there is a way to "crack" a private variable into visibility.

I know, for example, I've encountered warnings from the compiler when I've moved a property from lower visibility (in the base class) to a more visible level (in my descendant). The warning stated that it's being declared at a different level of visibility...

It's been some time and I'm not certain, but I believe what you can do is in your descendant declare the same variable as protected. (You may have to use the Redeclare keyword for this to compile.)

Sorry I don't have more specific information on how to do this (if it's indeed possible.) Perhaps this posting will prompt one of the wizards here into correcting me! :-)

-1

Expose the value of the private variable by a protected property in TThirdPartyComponent.

TThirdPartyComponent = class (TThirdPartyComponentBase)
private
   Procedure SetValue(Value: Integer);
   Function GetValue: Integer;
protected
   Property MyVar: Integer read GetValue write Setvalue; 
   procedure Foo; virtual;
end;

Procedure TThirdPartyComponent.SetValue(Value: Integer);
begin
  FSomeVar := Value ;
end;

Function GetValue: Integer;
begin
  result := FSomeVar;
end;

In TMyFixedComponent class use the MyVar Property in the procedure which you would like to override.

1
  • 5
    But this will change the original code of TThirdPartyComponent. I wanted some solution without rewriting TThirdPartyComponent's code or changing the original component's unit.
    – Andrew
    Oct 28, 2010 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.