Question Tags and Objects

Scribbly

Member
Joined
Jul 16, 2007
Messages
9
Programming Experience
3-5
I thought this would work, but it doesn't seem to...

I have an List of Objects called myList

I have a TreeView that I populate using the list, something like:
VB.NET:
for each a in myList
  Dim n as new TreeNode
  n.Tag = a
  n.Text = a.Text
  TreeView.Add(n)
next

When I edit an object, I create a new object and copy the data over and put it in the Form.Tag (I use a copy so that changes can be backed out from).

So when the changes are saved back to the original object, I do something like:
VB.NET:
TreeNode.Tag = editedObject

I thought that the object in the treeNode Tag was just a reference to the object in the List and changing one would change the other. But that's not happening? The object in the TreeNode.Tag is changed, but the object in the List is not??
 
If you change the same object referenced by Tag or List "both" is changed (not "both" actually since it is the same object:)), for example by changing a.Text.
If you assign a new object to Tag, then this is not the same object referenced in List.
 
OK, what I have to do is like a clone in reverse then?

So instead of

VB.NET:
TreeNode.tab = editedObject

it needs to be:

VB.NET:
TreeNode.tag.Text = editedObject.Text

Makes much more sense now, thanks
 
Back
Top