Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

The miracle of Tag

0.00/5 (No votes)
17 Jun 2007 1  
How to use the Tag property.

Introduction

Not a lot of people realize how powerful the Tag property actually is... (For people coming from other languages like Delphi where the Tag property is a String, using the Tag as an Object opens lots of opportunities.)

I will give you a quick overview and show you how you can use it.

Using the code

The Tag property is of type Object, making it a very powerful to work with. An Object is like the seed of OO.

Here is an example of how you can use it:

First, you will need to create a class with all the details you want to save.

public class Person
{
    public string Name;
    public string Surname;
}

Then, you populate it and link it to any Tag property. As easy as that:

// Create an temporary Person object
Person TempPerson = new Person();

// Populate it
TempPerson.Name = "Jack";
TempPerson.Surname = "The-Man!!!";

//Link the Temp object to the Tag
this.Tag = TempPerson;

// Get the Data back from the Tag
this.Text = 
    ((Person)this.Tag).Name + 
    " " +
    ((Person)this.Tag).Surname;

With the above code, I use the the Form (this) and link an object of any kind to the Tag property. Very handy!!!

In your day to day apps, you can use this to save limitless data on any Tag property (create a Node Index for example, and or link all the info you may need to a Tree Node.)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here