dummzeuch 1505 Posted June 25, 2019 (edited) Given the following class: unit Ancestors; interface type TAncestorClass = class private procedure SomeMethod; end; [... stuff ...] ... and a descendant class that is declared in a different unit: unit Descendants; interface uses Ancestors; type TDescendant = class(TAncestor) protected procedure SomeOtherMethod; end; // [...] implementation procedure TDescendant.SomeOtherMethod; begin SomeMethod; // <-- this won't compile end; I need to call the private TAncestor.SomeMethod from TDescendant.SomeOtherMethod. This of course won't compile because TAncestor.SomeMethod is declared as private and I cannot change that declaration (It's part of the Delphi VCL). My solution so far is to copy the implementation to my unit, but I am pretty sure this would violate Embarcadero's copyright on that (non trivial) code if I were to release that source code. So I am looking for a different solution. It guess it would be possible to somehow patch my class to call the original code, but I have never done something like that. Is there any documentation / blog post / example code on how to do that? I am aware that I would then possibly have to adapt this patch to every Delphi version. Any hints? Edit: Note: Class Helpers and extended RTTI are not an option here. This must work with Delphi 2007. Edited June 25, 2019 by dummzeuch Share this post Link to post
pyscripter 689 Posted June 25, 2019 (edited) Use the magic with statement: With Self do SomeMethod This works in all version of Delphi including recent ones. Edited June 25, 2019 by pyscripter Share this post Link to post
dummzeuch 1505 Posted June 25, 2019 23 minutes ago, pyscripter said: Use the magic with statement: With Self do SomeMethod This works in all version of Delphi including recent ones. Unfortunately that only works in class helpers. Share this post Link to post
dummzeuch 1505 Posted June 25, 2019 40 minutes ago, Larry Hengen said: Could this be your solution? I'll check it out, thanks. Share this post Link to post
Guest Posted June 25, 2019 I remember being a bit chocked when Emba changed that. You can still do it with "protected". However, i seldom port big (deployed) projects between delphi versions. And after a couple of years, the immediate thought [this should be so] obviously shows. For newer projects, i have been forced to suggest a move of fields or methods to vendors. That is normally not a big issue (pls move that up). And in the long run the result(s) are much cleaner. IMHO: A project that NEEDS to override private members should not be migrated to newer versions. As long as we are aware where this hack has been used, it should be an important consideration when deciding to port. I know this won't help you, but anyways... Share this post Link to post
Remy Lebeau 1396 Posted June 25, 2019 https://stackoverflow.com/a/36716715/65863 Share this post Link to post
dummzeuch 1505 Posted June 25, 2019 3 minutes ago, Remy Lebeau said: https://stackoverflow.com/a/36716715/65863 That works only for private fields, not for private methods. Share this post Link to post
pyscripter 689 Posted June 25, 2019 9 hours ago, dummzeuch said: Unfortunately that only works in class helpers. You can use a class helper to expose a private method of a parent class. 1 Share this post Link to post
dummzeuch 1505 Posted June 26, 2019 3 hours ago, pyscripter said: You can use a class helper to expose a private method of a parent class. No, I can't. 14 hours ago, dummzeuch said: Note: Class Helpers and extended RTTI are not an option here. This must work with Delphi 2007. Edited 14 hours ago by dummzeuch Share this post Link to post