Jump to content
Andrew Spencer

Accessing protected symbols

Recommended Posts

I've often used a derived class on TCustomControl to access a control's Canvas property.

type

  TGetCanvas = Class(TCustomControl);

and then used it later in a line similar to this:

TGetCanvas(progressPanel).Canvas.TextWidth("Hello World")

In trying to neaten things up, I noticed that the same type declaration was in the interface or implementation section of numerous units. I tried moving the declaration to the interface section of a "library" unit, and useing that unit in all these other units.

On compilation, however, a line like the one above would raise and error "E2362 Cannot access protected symbol TCustomControl.Canvas".

 

Why is this not functioning in the same way as if the type declaration was in the unit itself?

Share this post


Link to post

 

11 minutes ago, Andrew Spencer said:

Why is this not functioning in the same way as if the type declaration was in the unit itself?

That's for historical reasons. And with the "new" strict keyword even this can be prevented.

If you only want to access the Canvas property you can change your TGetCanvas class to this:

type
  TGetCanvas = Class(TCustomControl)
  published
    property Canvas
  end;  

This is used by all the classes that derive from TCustomControl to publish protected Canvas property, so it will always work.

  • Like 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×