473,385 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Tag Property

How can I achieve something like the following?

private void label_Click(object sender, System.EventArgs e)
{
Label label = (Label)sender;
int number = (int)label.Tag;
}

The second line causes a System.InvalidCastException to be thrown.

Nine Labels have their Click event handlers set to the above method.
Each has its Tag property set at design-time to a number from 0 to 8.

I've tried searching MSDN for info about the Tag property in
who-knows-what-base-class, but it's like trying to find a needle in a
haystack.
Nov 15 '05 #1
7 23045
Hi!

Tag is inherited from the Control class and is System.Object type.

That said, you could do a convert using the Convert class as shown below:

int number = Convert.ToInt32(label.Tag);

--
Regards,
Kumar Gaurav Khanna
-----------------------------------------------------------------
Microsoft MVP - C#/.NET, MCSE Windows 2000/NT4, MCP+I
WinToolZone - Spelunking Microsoft Technologies
http://www.wintoolzone.com/
OpSupport - Spelunking Rotor
http://opsupport.sscli.net/
Bangalore .NET Users' Group
http://groups.msn.com/bdotnet/
"C# Learner" <cs****@learner.here> wrote in message
news:eo********************************@4ax.com...
How can I achieve something like the following?

private void label_Click(object sender, System.EventArgs e)
{
Label label = (Label)sender;
int number = (int)label.Tag;
}

The second line causes a System.InvalidCastException to be thrown.

Nine Labels have their Click event handlers set to the above method.
Each has its Tag property set at design-time to a number from 0 to 8.

I've tried searching MSDN for info about the Tag property in
who-knows-what-base-class, but it's like trying to find a needle in a
haystack.

Nov 15 '05 #2
"Gaurav Khanna [C# MVP]" <ga****@wintoolzone.com> wrote:
Hi!

Tag is inherited from the Control class and is System.Object type.

That said, you could do a convert using the Convert class as shown below:

int number = Convert.ToInt32(label.Tag);


That works, thanks.

I don't understand why I can't just cast though. I mean, isn't Tag
made for holding a reference to an object (i.e. an address)? In that
case, wouldn't it just be holding 0x01?

If this is true, it's just holding binary 0000 0001, which should be
"castable", shouldn't it?
Nov 15 '05 #3
C# Learner <cs****@learner.here> wrote:

<snip>
In that
case, wouldn't it just be holding 0x01?


I meant, "For example, if the Tag property is set to 1 at design-time,
wouldn't it just be holding 0x01?"

<snip>
Nov 15 '05 #4
C# Learner,
It really depends on how the designer set the value to 1.

Was it 1 (an integer) or was it "1" (a string)?

It its "1" a string, then the Convert.ToInt32 is the way to go, however if
its truly 1 an integer, then your cast should work. Seeing as your cast was
failing I suspect its "1" a string.

You could put a breakpoint on the failing statement, and use the watch
window to verify what type of value the Tag property is actually holding.

Hope this helps
Jay

"C# Learner" <cs****@learner.here> wrote in message
news:ud********************************@4ax.com...
C# Learner <cs****@learner.here> wrote:

<snip>
In that
case, wouldn't it just be holding 0x01?


I meant, "For example, if the Tag property is set to 1 at design-time,
wouldn't it just be holding 0x01?"

<snip>

Nov 15 '05 #5
The problem is when you type "1" in the designer, it is assuming that "1" is
a string. Therefore, the Tag property does not hold a number, but an
instance of System.String. So in fact Tag actually contains a pointer to a
string "1" somewhere.

System.String cannot be directly cast to an int. Even if it COULD it
wouldn't work in this case.

The reason is due to boxing/unboxing--casting an Object to int will ONLY
work when that Object actually contains a boxed int.

For example, you can do this:

int a = 5;
short b = a;

And you can do this:

int a = 5;
object o = a;
int b = (int)o;

But you cannot do this:

int a = 5;
object o = a;
short b = (short)o;

To make the above code work, you would have to change it to:

int a = 5;
object o = a;
short b = (short)(int)o;

Or

int a = 5;
object o = a;
short b = Convert.ToInt16(o);

The Covnert class handles the casting and unboxing for you, as well as
conversions from strings to numbers (at the expense of
performance--especially if you already know the datatype of the boxed
value).

If you REALLY need to store an int directly in the Tag, you need to modify
the code that the Form designer generated for you. Somewhere in
InitializeComponent() you will see a line like this:

label.Tag = "1";

Which you could change to:

label.Tag = 1;

However, the designer will probably just change it back first chance it
gets. You could also set the value elsewhere (in the Form constructor for
example).

But both of these methods would defeat the purpose of the designer.
Incidentally, one could write a component similar to the ToolTipProvider
that added an IntTag to every control, but I don't think it's worth the
trouble.
I think the best solution is Convert.ToInt32().

However, if you're worried about performance, then:

Int32.Parse((string)label.Tag)

might run a bit faster....but you still have a string-to-int conversion
which isn't all that fast (relatively) to begin with.
Enough babbling on my part though.

--Matthew W. Jackson

"C# Learner" <cs****@learner.here> wrote in message
news:ud********************************@4ax.com...
C# Learner <cs****@learner.here> wrote:

<snip>
In that
case, wouldn't it just be holding 0x01?


I meant, "For example, if the Tag property is set to 1 at design-time,
wouldn't it just be holding 0x01?"

<snip>

Nov 15 '05 #6
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote:
C# Learner,
It really depends on how the designer set the value to 1.

Was it 1 (an integer) or was it "1" (a string)?

It its "1" a string, then the Convert.ToInt32 is the way to go, however if
its truly 1 an integer, then your cast should work. Seeing as your cast was
failing I suspect its "1" a string.

You could put a breakpoint on the failing statement, and use the watch
window to verify what type of value the Tag property is actually holding.
Oops! You are indeed correct: the code created by the designer is
assigning it a string value. I should've looked in
InitializeComponent()!
Hope this helps
Jay


Thanks
Nov 15 '05 #7
"Matthew W. Jackson" <th********************@NOSPAM.NOSPAM> wrote:

<snip>
However, if you're worried about performance, then:

Int32.Parse((string)label.Tag)

might run a bit faster....but you still have a string-to-int conversion
which isn't all that fast (relatively) to begin with.
I'm not so worried about performance in this case, and
Convert.ToInt32() will do.
Enough babbling on my part though.
Thanks for sharing!
--Matthew W. Jackson


Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Johnny M | last post by:
using Access 2003 Pardon the subject line, but I don't have a better word for this strange behavior (or behavior I don't understand!!!) I have a class module named DepreciationFactor. One of...
2
by: Edward Diener | last post by:
How does one specify in a component that a property is a pointer to another component ? How is this different from a property that is actually an embedded component ? Finally how is one notified in...
0
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control...
3
by: Marty McFly | last post by:
Hello, I have a control class that inherits from System.Web.UI.WebControls.Button. When I drag this control from the "My User Controls" tab in the toolbox onto the form, I want it to reflect the...
15
by: Sam Kong | last post by:
Hello! I got recently intrigued with JavaScript's prototype-based object-orientation. However, I still don't understand the mechanism clearly. What's the difference between the following...
27
by: sklett | last post by:
I just found myself doing something I haven't before: <code> public uint Duration { get { uint duration = 0; foreach(Thing t in m_things) { duration += t.Duration;
15
by: Lauren Wilson | last post by:
Owning your ideas: An essential tool for freedom By Daniel Son Thinking about going into business? Have an idea that you think will change the world? What if you were told that there was no way...
0
by: Memfis | last post by:
While I was looking through the group's archives I came across a discussion on doing properties (as known from Delphi/C#/etc) in C++. It inspired me to do some experimenting. Here's what I came up...
0
ADezii
by: ADezii | last post by:
The motivation for this Tip was a question asked by one of our Resident Experts, FishVal. The question was: How to Declare Default Method/Property in a Class Module? My response to the question was...
1
by: cday119 | last post by:
I have a Class with about 10 properties. All properties return right except for one. It is real annoying and I can't see why its not working. Maybe someone else can see something. It is the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.