CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Nov 2002
    Posts
    34

    could someone explain the difference to me

    Hi all could someone explain why is this happening?

    Ive got a first piece of code:

    Code:
    Private Function Clone2(ByVal s As String) As String
            clone2 = s
    End Function
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    dim s as string
    dim ss as string
    
    s = "asdf"
    ss = clone2(s)
    ss = "zxcv"
        End Sub
    Which happily makes value of s = "asdf" and value of ss = "zxcv"

    now have a look at another peice of code

    Code:
    Public Function Clone2(ByVal tn As TreeNode) As TreeNode
            clone2 = tn
        End Function
    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    dim tn as new treenode()
    dim tnn as treenode
    
    tn.text = "asdf"
    tnn = clone2(tn)
    tnn.text = "zxcv"
        End Sub
    Which makes tn.text and tnn.text = "zxcv"


    Why????

  2. #2
    Join Date
    Oct 2002
    Location
    Växjö, Sweden
    Posts
    225
    Because String variables are of Value type and objects (treenodes, textboxes....whatever) are reference types. Wich means that in the second case you are just saying that tn and tnn is pointing to the same object in memory. You then have to clone the object, but what you are doing is not a proper clone. There is a clone for the treenode class, why do what you did?

    Code:
    Dim tn As New TreeNode()
    tn.Text = "abc"
    Dim tnn As TreeNode
    tnn = tn.Clone
    tnn.Text = "def"
    MsgBox(tn.Text)

  3. #3
    Join Date
    Oct 2002
    Location
    Växjö, Sweden
    Posts
    225
    Originally posted by Athley
    Because String variables are of Value type and objects (treenodes, textboxes....whatever) are reference types. Wich means that in the second case you are just saying that tn and tnn is pointing to the same object in memory. You then have to clone the object, but what you are doing is not a proper clone. There is a clone for the treenode class, why do what you did?

    Code:
    Dim tn As New TreeNode()
    tn.Text = "abc"
    Dim tnn As TreeNode
    tnn = tn.Clone
    tnn.Text = "def"
    MsgBox(tn.Text)
    I guess this will still work though

    Code:
    Private Function Clone2(ByVal tn As TreeNode) As TreeNode
            clone2 = tn.clone
    End Function
    /Leyan

  4. #4
    Join Date
    Nov 2002
    Posts
    34
    Guys,

    got it, solved my problem (while writing you a reply ;-) which was if anyone interested...

    I've got a treenode subclass.
    Code:
    Class DomainNode
            Inherits TreeNode
            Public DomainRow As DataRow
            Public Sub New(ByVal DRow As DataRow)
                MyBase.new()
                Me.Text = DRow.Item("Name")
                DomainRow = DRow
            End Sub
        End Class
    
    Private Sub TreeView1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TreeView1.DragDrop
     Dim pt As Point
            Dim DestinationNode As DomainNode
            Dim OriginalRow, DestinationRow As DataRow
            Dim OriginalNode As DomainNode = CType(e.Data.GetData("OntosNavigator.Navigator+DomainNode"), TreeNode)
            pt = CType(sender, TreeView).PointToClient(New Point(e.X, e.Y))
            DestinationNode = CType(sender, TreeView).GetNodeAt(pt)
            If IsDropAllowed(OriginalNode, DestinationNode) Then
                DestinationNode.Nodes.Add(OriginalNode.Clone)            DestinationNode.Expand()
                OriginalNode.Remove()
    end if
    now i'm implementing a dragndrop operation. for some reason whenever i've tryed to clone an original node it was just exiting of sub (without any error msg.) been new to this type of programming i thought that i have to write an override to clone method in my new class. that is where all the confusion started....

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

Featured