Click to See Complete Forum and Search --> : could someone explain the difference to me
ilk
December 18th, 2002, 03:29 AM
Hi all could someone explain why is this happening?
Ive got a first piece of 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
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????
Athley
December 18th, 2002, 04:07 AM
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?
Dim tn As New TreeNode()
tn.Text = "abc"
Dim tnn As TreeNode
tnn = tn.Clone
tnn.Text = "def"
MsgBox(tn.Text)
Athley
December 18th, 2002, 04:09 AM
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?
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
Private Function Clone2(ByVal tn As TreeNode) As TreeNode
clone2 = tn.clone
End Function
/Leyan
ilk
December 18th, 2002, 04:24 AM
Guys,
got it, solved my problem (while writing you a reply ;-) which was if anyone interested...
I've got a treenode subclass.
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....
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.