Re: Generic components with published properties.

Giganews Newsgroups
Subject:Re: Generic components with published properties.
Posted by: Rob Kennedy (kennedyri+codege…@gmail.com)
Date:Fri, 3 Oct 2008

Ken White wrote:
> Daniel Bartlett wrote:
>> If I write button that has a generic property, then install it into Delphi, the published properties that are generic won't appear in the object inspector.
>>
>>  TGenericButton<T> = class(TButton)
>>  private
>>    FASomething: T;
>>    FAnInteger: Integer;
>>  published
>>    property AnInteger: Integer read FAnInteger write FAnInteger;
>>    property ASomething: T read FASomething write FASomething;
>>  end;
>
> What would you expect it to show in the Object Inspector for property ASomething?

For that class, nothing. But that's not the class Daniel registered or
placed on his form. He registered TIntegerButton.

> When it's a Boolean property, Delphi can figure out to display "True" and "False", and use a combobox to do so because there are only two possible choices. When it's a set property, Delphi can display a list of the elements of that set and allow you to set them using a True/False combobox, because there are limited allowable values, and those values are either included or excluded from the set.
>
> A TCollection-based property can display a specialized entry in the Object Inspector, because Delphi can use RTTI to determine what to display. It can use the same type of RTTI to figure out what to display for the Collection's Items[].
>
> What does it use for a property ASomething of type T? What properties does a T have that can display in the Object Inspector? Can you list them, or explain how to edit them, or what the allowable values of that property of type T are? Neither can the Object Inspector.

Do you actually _know_ the Object Inspector doesn't know what properties
T has?

I think you're assuming the answer to an important question: Is there a
way to take an instance of a generic class and find out what type or
types it was instantiated with?

>> Also, if you use TIntegerButton = TGenericButton<Integer>, instead of TIntegerButton = class(TGenericButton<Integer>), it can't name the components correctly, you get an error when placing them onto a form "GenericButton<System.Integer>1 is not a valid component name"
>
> That's pretty apparent. Don't use generic types as visual components. They don't make sense, anyway.

Why not?

> What's a visual representation of a TGenericButton<Integer>?

The same as that of a TButton, obviously. TButton is its ancestor class,
and TGenericButton hasn't added any code to change its appearance.

> What in the world is a TGenericButton<Integer> for, and what does it do?

It's button that holds a property of type Integer named ASomething.

> How does it differ from a regular TButton - can you only use a number-shaped cursor to click on it, or only use integer values for its' Caption?

No, that's not what that code does at all.

> Trying to make visual controls generic is not a good idea.

Why not? Your only argument seems to be that you can't think of why
someone might want to do it.

> Can you explain what you're trying to accomplish?

The only question Daniel asked was whether he was seeing a bug. I think
the answer is definitely yes. Either generic classes aren't allowed to
have published properties, and the compiler should have rejected
TGenericButton's declaration, or generic class _are_ allowed to have
published properties, and the Object Inspector should display them like
any other property.

--
Rob

Replies

None

In response to

Re: Generic components with published properties. posted by Ken White on Fri, 3 Oct 2008