Reputation: 91
I'm trying to add an array object to TreeViewItem.Tag but I'm not sure if my syntax is correct.
String first = "first string";
String second = "second string";
item.tag = new object[] {first,second};
String result = ((Object[])item.Tag)[1].ToString();
Here's the result I'm getting instead of "second string".
Object[] Array
Hope you guys can help! Thank you so much in advance!
Upvotes: 0
Views: 48
Reputation: 74605
If you take a bit more of an object oriented view on things, your code will be less confusing:
//a class to hold some strongly typed data in the Tag
record TreeNodeTag(string Title, string Description);
void PopulateTreeNodes(..)
{
...
item.Tag = new TreeNodeTag("Door", "A heavy metal door");
...
}
void EnuemrateNodes(..)
{
...
var tnt = someTreeNode.Tag as TreeNodeTag;
MessageBox.Show(tnt.Description, tnt.Title);
...
}
Try to put related data together into a well named class with well named properties rather than slinging around object arrays that will leave your code full of weird and (apparently difficult to manage) casts, magic number indexers and other unreadable, not-self-documenting stuff
Upvotes: 1