views:

168

answers:

1

Hey everyone,

I've been working with C# for over a year now, and I was wondering what other functionality the tag attribute has of a control, till now each .net control I've used (buttons, labels, textboxes, gridview, etc.) have a tag attribute that you can define.

I only know you can put something in it like a string of text.

The visual studio description says:

User-defined data associated with the control

Does the tag do anything else than that? Describe it's content? And where do you use if for? Has it been of use to any of you?

+10  A: 

No, it does nothing in itself. It's up to you to use it however you want to. For example, you could use it to do a sort of simplified databinding, such that you put the user-visible text in a checkbox, and the value to store in the database (e.g. an enum value) in the tag. Then you retrieve it from the tag later on.

IME it's usually useful for "quick and dirty" scenarios where there are better ways of working, but they take a bit longer to code up - which is fine for throwaway code, but not ideal for a complex production system. That may not always be the case, of course.

Jon Skeet
Agree with quick and dirty. I think it's a legacy from Delphi (also designed by Anders Hejlsberg), and I use it myself (especially on TreeNodes or something), but it always feels like it's wrong :)
OregonGhost
So, in theory it works the same as a normal variable?
Pieter888
@Pieter888: Absolutely. It's just an extra property on the control which is ignored by all the framework code as far as I'm aware.
Jon Skeet
Thanks for the answer!
Pieter888
Are we sure that's a Delphi legacy, and not a classic VB legacy? I don't remember when I first saw the Tag property on controls in classic VB, but the latest it could have been was VB5. (And I seem to remember, though I could be wrong, seeing them in VB4.)
John Rudy
I actually found some use for the tag, I defined a default value for some textbox controls saved into the tag, when I call my setDefaults() function it changes the textbox.text value with the control's tag value. I'm starting to like tags :)
Pieter888