tags:

views:

730

answers:

6

Is there any specific purpose for the 'Tag' property of Delphi VCL components? I have Googled a few examples using it as, for example, a 'color' property or using the value as a pointer address, but is it 'good practice' to use it, or is it considered 'bad practice' as it ties the program logic to the UI?

+20  A: 

The "tag" property is there as a "cargo container" for whatever you might want to do with it.

Something it's often used for is in event handlers when you have a lot of similar components sharing one event handler. The event handler can find its caller and then query its tag value to get some more information about what it's supposed to be acting on.

EDIT:

Example: A calculator app might tag the number buttons with their respective numbers... silly and incomplete example, but you get the idea. The event handler could then pull the number to add into the display and accumulator right out of the tag instead of having to go figure out which button is meant to do what.

Carl Smotricz
+7  A: 

It is a place to add a piece of information to any component, even if you don't have the source for that component. It should be used carefully, because you can only use it once. Libraries should never use it for that reason.

Henk Holterman
+2  A: 

As others have said, it's a place to put anything. Typically this comes in handy when associating two objects via an object reference or pointer. The tag happens to be perfectly sized to hold a pointer, so if you need to, say, keep an object tied to an item in a listbox, it becomes pretty straightforward.

David Lively
and hopefully a pointer will still fit in a tag when 64-bit Delphi arrives...
frogb
+2  A: 

Also it can be used for grouping purposes, say you'd want to access all components with a specific tag value, regardless of the component's type.

luvieere
+1  A: 

It's great! A freebie. I use it all the time to store one additional piece of information associated with the object.

Often I store a pointer to an associated data structure, or sometimes an integer which may be an index into some other array.

You can use it as a counter for times the object is accessed, or whatever.

The only downside is if your program uses lots of memory and you have millions of objects, those 4 bytes for each tag add up, especially if you're not using it. In that case, for your most prolific object type, you may want to create your own version without the tag.

lkessler
That's not likely to be a serious problem, as your "millions of objects" are very unlikely to have the Tag property. Only VCL classes have it, and you don't tend to have all that many of them instantiated at once.
Mason Wheeler
Oh you're right. Most tree and list items in standard Delphi don't have tags. I was mixing that up with the ElTree package by LMDInnovative that I use, which has a Tag on every item in the tree. My trees can have millions of items, and as a result, I have millions of tags. But the real point is that I love having that capability
lkessler
Tree and list items have a 'data' property, which is equivalent to the 'tag'.
No'am Newman
+1  A: 

You have 2 buttons on your form, on one you set the Tag = 1, and the other one Tag = 2. Now you assign the same OnClick event to both buttons and writhe the code like this:

procedure TForm28.Button1Click(Sender: TObject);
begin
  case (Sender as TButton) of
   1: Caption := 'you pressed button 1';
   2: Caption := 'you pressed button 2';
  end;
end;

or more compact:

procedure TForm28.Button1Click(Sender: TObject);
begin
  Caption := 'you pressed button ' + IntToStr((Sender as TButton).Tag);
end;

Basically,Tag will let you identify what control fired the event. Think if you have a form with dynamically created buttons... a list with users from the database, and on each record you put a button "Delete User". In this situation you can't create an event for each button, you will create one event that will assigned to all the buttons... and you can put in the Tag the userid for example. That way when you implement the event to handle all the buttons, you'll know what user to delete.

ioan