Tag

Sometimes in a WinForm, you need to associate a bit of data with a UI control. Control.
Tag then becomes useful.

I’m sorry, but I disagree. It is my opinion that the Tag property should be buried as soon as possible.
Consider the next person in line to maintain your application. The Tag property is untyped and unnamed, and could cause so much confusion, that it’s not worth it.
My solution? Created a derived class for the control and create a property for your information. In my opinion, it’s more intuitive.

Advertisements

6 thoughts on “Tag

  1. I disagree: It’s very easy to get the Type of an object stored in a tag, and in most cases the way that the Tag is used is consistent across an application. I find it extremely useful in a treeview situation, where I can bind the object a node represents to the node itself.

  2. Chris- Useful? Yes, of course. Problem is that you can’t tell the object’s type until runtime. By than, it might be too late. ;)
    You could just extend the TreeNode class with this single line of code:

    public class MyTreeNode : System.Windows.Forms.TreeNode { private object m_BoundValue; public object BoundValue { get { return m_BoundValue; } set { m_BoundValue = value; } } }

    It’s astounding what you can do with a single line of code, isn’t it? ;P

    About the part that the Tag property is consistent across an application… Maybe if you’re one developer…

    In an application I saw that had 4 developers, none of them used it in the same fashion… Some even used it differently in two parts of their own code.

  3. No offense, but I get so tired or hearing all the purists I know going off about "strongly-typed" this and "strongly-typed" that. Its strong typing everything thats makes it a nightmare when you decide you want to change what gets stored with your treeview nodes, and you have to go change code in a million different places. Development time and time spent on enhancements later can be huge. I personally have never saved that many bugs by making everything strongly typed. Really think about your experiences here. Its overrated.

    People using it in the way you describe should probably find new careers. I leave it to my fellow developers to be intelligent enough to figure out how a loosly type object is used, and follow in the same usage. If they can’t do that, either they need to find new work, or I need to write better comments to support by code.

  4. Ron-
    "Its strong typing everything thats makes it a nightmare when you decide you want to change what gets stored with your treeview nodes, and you have to go change code in a million different places."

    I’ll grant you that. You would have to change the code in a million places. On the other hand, this makes sure you KNOW where you changed the code. This helps you kill all those little code bugs just waiting to happen.

    "I personally have never saved that many bugs by making everything strongly typed."

    I honestly have.

    "I leave it to my fellow developers to be intelligent enough to figure out how a loosly type object is used, and follow in the same usage."

    I never claim that people can’t tell how the Tag property is used in the code, but it would take them a hell of a lot of time to do that. Time you could do by writing a few simple lines of code to extend the original class.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s