|
-
Feb 1st, 2005, 07:03 PM
#1
Thread Starter
Addicted Member
Treeview Tag and object
I want to store a structure in the tag field of a treenode.
I create the structure, assign values to variables in the structure and then assign the structure to the node. This all works great untill I want to change a value within the structure by access the node.
I get the error "Expression is a value and cannot be a target of an assignment" when I try the code below
CType(nde.Tag, SG_DATA).AllowableID = row.AllowableSpeciesID
and if I try the following ...
sgData = CType(nde.Tag, SG_DATA)
sgData.AllowableID = row.AllowableSpeciesID
At runtime the value of the AllowableSpeciesID is changed in structure object sgData but not in the node tag object. It seems as if I cannot get a reference to the stored object.
Is there something I am missing?
Any suggestions....
Control Data Systems
www.members.shaw.ca/cdsystems
-
Feb 1st, 2005, 07:18 PM
#2
Re: Treeview Tag and object
Try doing the same but use a class instead of a structure. That way, you'll get a reference to the object stored in the node tag. My guess would be that sgData = CType(nde.Tag, SG_DATA) creates a copy of the structure instead of passing you a reference to it, something that doesn't happen for an object.
Cheers,
NTG
-
Feb 1st, 2005, 07:19 PM
#3
Re: Treeview Tag and object
A structure is a Value not a class, so basically when you access or assign it, you're accessing/assigning a copy of it, not a reference to it, so you need to get the structure value from the Tag, alter it, then reassign it to the Tag, i.e.
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
Dim node As TreeNode
Dim struct As MyStruct
For i = 1 To 10
node = TreeView1.Nodes.Add("Node " & i)
struct.Property1 = "Node " & i
node.Tag = struct
Next
End Sub
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
Dim struct As MyStruct = e.Node.Tag
TextBox1.Text = struct.Property1
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim struct As MyStruct
If Not TreeView1.SelectedNode Is Nothing Then
struct = TreeView1.SelectedNode.Tag
struct.Property1 = TextBox1.Text
TreeView1.SelectedNode.Tag = struct
End If
End Sub
Public Structure MyStruct
Public Property1 As String
End Structure
-
Feb 1st, 2005, 08:33 PM
#4
Thread Starter
Addicted Member
Treeview Tag and object - resolved
Thanks guys, that is exactly the problem.
I am getting a new version of object and I do not get a reference to the structure. So the solution is just to reassign it, after I have changed it.
Thank for the great help.
Control Data Systems
www.members.shaw.ca/cdsystems
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|