Mega Code Archive

 
Categories / Delphi / OOP
 

Accessing protected class properties

Title: Accessing protected class properties Question: Sometimes you need to access properties of classes declared elsewhere that are protected. Answer: The simplest way to achieve this is to use the following declaration in your unit: TExposed = class(); For example: TExposedWinControl = class(TWinControl); Since TExposedbutton is now declared in your unit, you can access everything that is protected -- including whatever is inherited. How to use it? Safely hard-cast it as follows: if Sender is TWinControl then with TExposedWinControl(Sender) do begin //Caption & Text is the same in TWinControl ShowMessage(Caption); Text := 'Easy, isn''t it?'; end;