Caves Travel Diving Graphics Mizar Texts Cuisine Lemkov Contact Map RSS Polski
Trybiks' Dive Texts Programming Accessing Protected Members YAC Software
  Back

List

Charsets

Charts

DBExpress

Delphi

HTML

Intraweb

MSTest

PHP

Programming

R

Rhino Mocks

Software

Testing

UI Testing

VB.NET

VCL

WPF

Accessing Protected Members
Let's assume that you need to access some of the protected properties (or methods) in a component's instance. Of course, you could create a new class that extends the original class, make the protected members public, and instantiate the new class instead.

However, in some situations, this could be either too complicated or just not possible. For instance, if you need access to a protected property for only one or two operations in a standard component, creating a new component, registering it, and then managing it could be just too cumbersome (I never got to like Delphi's component management). Or maybe the class is instantiated outside of your code and you have no way of changing that code?

The code below is a bit of a hack (or rather a shortcut), but in situations like those mentioned above works quite nicely. Let's take, for instance, TPanel and its protected property Canvas. You can expose this member as follows:
  type
    TCanvasPanel = class(TPanel)
    public
      property Canvas;
    end;
  
  ...
    // Now let's say that Panel is instantiated outside of your code,
    // but you still need access to its Canvas:
    TCanvasPanel(Panel).Canvas.Ellipse(10, 10, 50, 50);
  ...
    // However the following instruction will not work -
    // a runtime error will be reported (EInvalidCast):
    (Panel as TCanvasPanel).Canvas.Ellipse(10, 10, 50, 50);
  ...
That's Delphi, but you should be able to do this in other languages that allow type casts without too strict type checking.

Top

Comments
Alas!
No comments yet...

Top

Add a comment (fields with an asterisk are required)
Name / nick *
Mail (will remain hidden) *
Your website
Comment (no tags) *
Enter the text displayed below *
 

Top

Tags

Programming

Delphi


Related pages

TFS - The underlying connection was closed: an unexpected error occurred on a receive.

WCF - The underlying connection was closed: an unexpected error occurred on a receive.

Delphi interfaces... again

Saving / restoring window placements in .NET

Checking "Dangling" Event Handlers in Delphi Forms

Meaningful identifiers

Public fields vs. properties

Drag-n-drop files onto the application window

Intraweb and MaxConnections

A Case for FreeAndNIL

Intraweb as an Apache DSO module

"Device not supported" in Intraweb

Automated GUI Testing

Random()'s Determinism

Rounding and precision on the 8087 FPU

SessionTimeout in Intraweb

Using TChart with Intraweb

Unknown driver: MySQL

TIdMessage's CharSet

Software Guarantees

Automated Testing of Window Forms

TChart - Missing Labels in Axes

Memory Leaks and Connection Explosions in DBExpress

Controlling Conditional Defines and Compilation Switches

Detecting Memory Leaks with DUnit

last_insert_id() and DBExpress

Registering Extensions

DBExpress and Thread Safety

Forms as Frames

Checking Dangling Pointers vs. the New Memory Manager

Objects, interfaces, and memory management in Delphi