Results 1 to 4 of 4

Thread: Treeview Tag and object

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    San Diego - California
    Posts
    251

    Angry 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

  2. #2
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    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
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  3. #3
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    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:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.       Dim i As Integer
    3.       Dim node As TreeNode
    4.       Dim struct As MyStruct
    5.  
    6.       For i = 1 To 10
    7.          node = TreeView1.Nodes.Add("Node " & i)
    8.          struct.Property1 = "Node " & i
    9.          node.Tag = struct
    10.       Next
    11.    End Sub
    12.  
    13.    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
    14.       Dim struct As MyStruct = e.Node.Tag
    15.       TextBox1.Text = struct.Property1
    16.    End Sub
    17.  
    18.    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    19.       Dim struct As MyStruct
    20.  
    21.       If Not TreeView1.SelectedNode Is Nothing Then
    22.          struct = TreeView1.SelectedNode.Tag
    23.          struct.Property1 = TextBox1.Text
    24.          TreeView1.SelectedNode.Tag = struct
    25.       End If
    26.    End Sub
    27.  
    28.    Public Structure MyStruct
    29.  
    30.       Public Property1 As String
    31.  
    32.    End Structure

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2000
    Location
    San Diego - California
    Posts
    251

    Wink 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
  •  



Click Here to Expand Forum to Full Width