Omer van Kloeten's .NET Zen

Programming is life, the rest is mere details

News

Omer van Kloeten's Facebook profile

Omer has been professionally developing applications over the past 8 years, both at the IDF’s IT corps and later at the Sela Technology Center, but has had the programming bug ever since he can remember himself.
As a senior developer at NuConomy, a leading web analytics and advertising startup, he leads a wide range of technologies for its flagship products.

Get Firefox


powered by Dapper 

.NET Resources

Articles :: CodeDom

Articles :: nGineer

Culture

Projects

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.

Comments

AndrewSeven said:

I am quite surprised that still exists.
# May 21, 2004 12:35 PM

Chris McKenzie said:

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.
# May 21, 2004 1:10 PM

Omer van Kloeten said:

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.
# May 21, 2004 1:31 PM

Ron Shelton said:

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.
# May 21, 2004 1:49 PM

Omer van Kloeten said:

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.
# May 21, 2004 1:57 PM

Giampiero said:

What if you had something like Node<T> where <T> was the type of the tag?
# June 3, 2004 4:53 PM

Omer van Kloeten said:

Giampiero,

The issue is the fact that Tag is generic. What does it stand for? You would have to find out for yourself, even if you knew the type.

I remember Tag from my VB6 days. Back then it was needed because you couldn't derive from the class and make something of your own.
# June 3, 2004 4:58 PM

TrackBack said:

# June 18, 2004 1:32 AM

TrackBack said:

# June 22, 2004 7:18 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)