[fpc-devel] Propose: change TComponent.Tag from longint to PtrInt

Bram Kuijvenhoven kuifwaremailinglists at xs4all.nl
Wed Sep 6 08:50:47 CEST 2006

Michael Van Canneyt wrote:
>> I don't consider storing a pointer in the tag is broken code, it was
>> considered the accepted way to maintain a pointer to your custom data 
>> from a
>> standard control and there is often no simple alternative.
> Accepted only because there was no other way.
> The only correct way is to introduce an additional field 'UserData', of 
> type Pointer.

I think this is an excellent solution. Casting Tags to pointer is/was bad practice anyway.

The Tag can still be used for association of user data with a component, but then as an index to a lookup table which holds the actual pointer. That is a portable solution (though it requires a little more memory and code). So adding a UserData field will be helpful.

If we really want to save memory usage, we can even let a UserData (public) property and the Tag (published) property use the same field. Tag will then be an interface to the lowest 32 bits of a pointer field, which is simply stored/read in a whole by UserData. But this is of course a hack which is more likely to confuse programmers and cause problems in the future, so I wouldn't particularly recommend it...


Bram Kuijvenhoven

More information about the fpc-devel mailing list