Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Hiding a base class property

1,482 views
Skip to first unread message

Farshad

unread,
Mar 22, 2008, 6:57:53 PM3/22/08
to
How can I hide a base property?

AFAIK, it is not possible to directly reduce visibility of a property from a
base class. It would be good if a new keyword such as "hide" could be
introduced. for example;

TNewClass = class(TBaseClass)
private
...
protected
...
hide
property Color;
porperty Len;
...
published
property Width;
...
end;

Wayne Niddery (TeamB)

unread,
Mar 22, 2008, 11:22:16 PM3/22/08
to
"Farshad" <far...@ATfmsoftDOT.net> wrote in message
news:47e58eef$1...@newsgroups.borland.com...

>
> AFAIK, it is not possible to directly reduce visibility of a property from
> a base class. It would be good if a new keyword such as "hide" could be
> introduced. for example;
>
> hide
> property Color;
> porperty Len;


Reasons this is not (and should not be) done:

1) On principle, public/published properties and methods of a class should
be considered a contract. Once given, it cannot be taken away. Those knowing
the ancestry of a class would rightly expect to be able to access
public/published inherited items.

2) In practice, allowing this would actually break code. Specifically it
would cause errors where the base class was being used polymorphically.
Consider the following example:

type
TBase = class
public
property Visible: boolean;
end;

TFoo = class(TBase)
end;

TBar = class(TBase)
end;

THide = class(TBase)
hide
property Visible;
end;

// fill a list with a number of TBase descendants

var base: TBase;
begin
for base in list do
base.Visible := True;

If the list contains any THide instances, the above blows up, but it should
not since it is *properly* depending on polymorphism.

--
Wayne Niddery - TeamB (www.teamb.com)
Winwright, Inc. (www.winwright.ca)

Alexandre Machado

unread,
Mar 24, 2008, 8:10:01 AM3/24/08
to

> 2) In practice, allowing this would actually break code. Specifically it
> would cause errors where the base class was being used polymorphically.

All code based on getting the class in runtime, for instance with
GetClass('TMyBusinessClass') would potentially fail as well.

Regards


Farshad

unread,
Mar 24, 2008, 9:24:24 AM3/24/08
to
Ok, thanks.

Actually I want to hide them only from the Object Inspector. i.e. I want to
hide published properties. It can still remain as public so its scope level
won't change.

My problem is that there are lots of published properties in ancestor that
makes a newly derived Component very hard to browse in Object Inspector.


"Wayne Niddery (TeamB)"

Michael Justin

unread,
Mar 24, 2008, 9:51:36 AM3/24/08
to
Farshad schrieb:

> Actually I want to hide them only from the Object Inspector. i.e. I want to
> hide published properties. It can still remain as public so its scope level
> won't change.
>
> My problem is that there are lots of published properties in ancestor that
> makes a newly derived Component very hard to browse in Object Inspector.

Hello Farshad

This could be solved with a 'Proxy' or 'Facade' style component, which
contains an embedded (private) instance of the real component and
publishes only the required properties and methods.

With SetSubcomponent(True) the embedded component could be shown as a
'nested' component in the inspector for optional fine-tuning.

Hope this helps (tm)
Michael Justin
--
betasoft - Software for Delphi™ and for the Java™ platform
http://www.mikejustin.com/
http://www.betabeans.de/

Farshad

unread,
Mar 24, 2008, 9:55:50 AM3/24/08
to

"Michael Justin"

> Hello Farshad
>
> This could be solved with a 'Proxy' or 'Facade' style component, which
> contains an embedded (private) instance of the real component and
> publishes only the required properties and methods.
>
> With SetSubcomponent(True) the embedded component could be shown as a
> 'nested' component in the inspector for optional fine-tuning.
>

Hello,

Thanks. I know but using above method will change base class of your
component. For example if it is a TDataSet descendant you won't be able to
plug it into a datasource.


Wayne Niddery (TeamB)

unread,
Mar 24, 2008, 10:17:30 AM3/24/08
to
"Farshad" <far...@ATfmsoftDOT.net> wrote in message
news:47e7...@newsgroups.borland.com...

>
> Actually I want to hide them only from the Object Inspector. i.e. I want
> to hide published properties. It can still remain as public so its scope
> level won't change.
>
> My problem is that there are lots of published properties in ancestor that
> makes a newly derived Component very hard to browse in Object Inspector.


Ok. Depends on the component you are descending from, but many of the VCL
controls have a "Custom" ancestor where the properties are just public, e.g.
you'll see that TEdit descends from TCustomEdit and the only thing TEdit
really does is promote public properties to published. In this case descend
from TCustomEdit and publish just those properties you want.

Farshad

unread,
Mar 24, 2008, 7:13:28 PM3/24/08
to

"Wayne Niddery (TeamB)"

> Ok. Depends on the component you are descending from, but many of the VCL
> controls have a "Custom" ancestor where the properties are just public,
> e.g. you'll see that TEdit descends from TCustomEdit and the only thing
> TEdit really does is promote public properties to published. In this case
> descend from TCustomEdit and publish just those properties you want.

I see. My Component is a descendant of TDataSet so really no way to go back
and use a Custom version. Really isn't there any way to hide a published
property? IIRC, I saw a 3rd party component, a descendant of TQuery with
less properties than the original TQuery. I need to remember its name and
examine its code.


Wayne Niddery (TeamB)

unread,
Mar 24, 2008, 11:58:56 PM3/24/08
to
"Farshad" <far...@ATfmsoftDOT.net> wrote in message
news:47e8...@newsgroups.borland.com...

>
> I see. My Component is a descendant of TDataSet so really no way to go
> back and use a Custom version.

You should've taken a look - in the case of TDataset, it has no published
properties except any inherited from TComponent. Only descendants publish
TDataset properties.

Vladislav Filyakov

unread,
Mar 25, 2008, 11:29:28 AM3/25/08
to
> How can I hide a base property?

DesignIntf.UnlistPublishedProperty in the latest Delphi versions.

Vlad

Farshad

unread,
Mar 25, 2008, 4:36:38 PM3/25/08
to
Yes, that's it!

Thank you very much for the info. That's the only way to hide a property.

Here is the comment from Borland in file DesignIntf.pas:

{ UnlistPublishedProperty
From time to time there is a need to hide a property that has been
published by
an ancestor class. Whenever this occurs you should make sure that you are
descending from the right class. We realize, though, that sometimes this
is not
feasible. The following procedure will therefore allow you to make a
specific
property on a specific class not appear in the Object inspector.

** Please note that this function does not stop the streaming system from
streaming the published property nor does it make the published property
from
begin programmatically access at runtime. It simply tells the object
inspector
not to list (and in turn edit) it when components of the specified class
are
selected. }

"Vladislav Filyakov" <vl...@devexpress.com> wrote in message
news:47e91a6c$1...@newsgroups.borland.com...

Joanna Carter [TeamB]

unread,
Mar 25, 2008, 5:04:46 PM3/25/08
to
Farshad a écrit :

> ** Please note that this function does not stop the streaming system from
> streaming the published property nor does it make the published property
> from
> begin programmatically access at runtime. It simply tells the object
> inspector
> not to list (and in turn edit) it when components of the specified class
> are
> selected. }

Note this well !!!

This technique does *not* hide a property, it only prevents a property
declared on a component from being seen in the object inspector at
design time.

I would reiterate that the requirement to hide public/published
properties in derived classes is symptomatic of very poor class design.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Henrick Hellström

unread,
Apr 1, 2008, 7:47:15 AM4/1/08
to
Farshad wrote:
> Thanks. I know but using above method will change base class of your
> component. For example if it is a TDataSet descendant you won't be able to
> plug it into a datasource.

Any data aware component will accept TDataSource descendants for their
DataSource property, so you could solve that problem by adding a
published MyDataSet: TMyDataSetWrapper property to a TMyDataSource =
class(TDataSource). The setter of that property would simply assign the
wrapped dataset to the DataSet property of the datasource.

Henrick Hellström

unread,
Apr 1, 2008, 7:52:50 AM4/1/08
to
Wayne Niddery (TeamB) wrote:
> var base: TBase;
> begin
> for base in list do
> base.Visible := True;
>
> If the list contains any THide instances, the above blows up, but it
> should not since it is *properly* depending on polymorphism.

FWIW and to the extent it at all makes sense to discuss unimplemented
and not fully specified semantics; I don't think so. The Visible
property is only hidden in THide. If you cast the instance to TBase (and
the above code does that), the Visible property would have the
visibility it has in TBase.

0 new messages