Board index » cppbuilder » accessing common class members in classes derived from different vcl controls

accessing common class members in classes derived from different vcl controls

a subset of my components (which are derived from different vcl base
classes) share a common member.
i want to iterate through all of them on some panel and access the common
member (if it is there) and without having to extend a switch cascade with a
downcast for every new component i create).

how can this be done in the most elegant way ?

 

Re:accessing common class members in classes derived from different vcl controls


Quote
Tobias Schwinger wrote:
> a subset of my components (which are derived from different vcl base
> classes) share a common member.
> i want to iterate through all of them on some panel and access the common
> member (if it is there) and without having to extend a switch cascade with a
> downcast for every new component i create).

> how can this be done in the most elegant way ?

I don't know about elegant, but here are some ideas...

If the components are for your use alone, you might make use of the Tag
property and use it as a pointer to the member.  Set it in the constructor.

If you only need to do this at run-time, you could override the DYNAMIC
public function GetNamePath().  As recent as BCB5, this method is not
called at run-time.  By checking for csDesigning in the ComponentState,
you could alternately call the inherited function or return the pointer or value
converted the to a string. That may not be elegant, but certainly invisible
and effective.

Otherwise, you might send a unique message to the component to which
only those having the "member" would respond appropriately.

Re:accessing common class members in classes derived from different vcl controls


Is the common member in question a published property/event, or is it a
method?  If it is a property/event, then since the components don't share a
common base class (from what it sounds like), then the most direct way is to
simply use the VCL's native RTTI (runtime type information) to determine
whether a component has a given property and if so then get/set it as
needed.

For example, if you go to
http://www.blong.com/Conferences/BorConUK98/C++RTTI/CB140.htm, there is a
nice example of setting the Enabled property for a list of compoents without
any common base classes using only a few lines of RTTI programming via the
functions found in the TypInfo.hpp header.

Gambit

Quote
"Tobias Schwinger" <schwin...@klest.de> wrote in message

news:3e466257@newsgroups.borland.com...
Quote
> a subset of my components (which are derived from different
> vcl base classes) share a common member.
> i want to iterate through all of them on some panel and access
> the common member (if it is there) and without having to extend
> a switch cascade with a downcast for every new component i create).

Other Threads