Thread: Windows forms use the "tag" variable...

  1. #1
    Registered User
    Join Date
    May 2004

    Windows forms use the "tag" variable...

    So a while ago, I was playing around with C# and designing forms, and I saw they had a really cool variable called "tag" that was in all of their form objects (buttons, text fields, labels, etc.).

    So for example, if you had some sort of networking app, wanted to store some per-button data such an IP address, you could go ahead and cram that into the tag field. You could even make entire classes to put into the tag field.

    I really liked this pattern, because my tag could travel around with the button, instead of me having to store somewhere a hash table from button-id to my would-be-tag class.

    I've looked around, and I've seen no mention of this being a pattern, or anyone talking about this variable. What do you guys think of it? Is it a pattern worth knowing and using?

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    I don't know what the name for this pattern is. But yes, the ability to "hang" a piece of data on an object is useful, though it can be abused. The problem is, what if some other code layer also wants to store data in the tag? This makes it impossible to use this pattern in reusable code, because you can't depend on having exclusive access to the tag.

    The one place it's normally appropriate is storing associated data on a ListViewItem or similar object where the whole point of the tag variable is this type of usage.
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008

    Sounds like a variation on "Decorator" to me with the target operations being "craft a component that targets this BLOB" and "give me my BLOB".


  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Middle of NoWhere
    I think, this is C++ forums...
    Wrong post???
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows 7, First Impressions
    By Mario F. in forum General Discussions
    Replies: 16
    Last Post: 08-11-2009, 06:46 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. accessing windows forms textbox properties
    By luigi40 in forum C# Programming
    Replies: 5
    Last Post: 04-19-2005, 04:53 PM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM