Im new to C# and I only want to know how to set an objects tag to a number for example
button a= new button();
a.parent=this;
...
a.tag=12; (but it says it must be an object and I dont want to pass an object to it , any int to object conversion?)

Recommended Answers

You can use a string, it is an object.
If you absolutely need an int, you can create a small class :

public class Tag {public int value = 0;}

that you use in your form :

Tag tag = new Tag {value …
Jump to Post

All 2 Replies

You can use a string, it is an object.
If you absolutely need an int, you can create a small class :

public class Tag {public int value = 0;}

that you use in your form :

Tag tag = new Tag {value = 12};
this.button1.Tag = tag;

No need to create a class, just force it to box the value:
this.button1.Tag = (object)12;

But remember, when you go to retrieve it, it will be of type Object, you'll have to cast it back into whatever you need it to be.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.