473,763 Members | 6,401 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Removing reference type members from a generic list clone

I have created a custom class with both value type members and reference type
members. I then have another custom class which inherits from a generic list
of my first class. This custom listneeds to support cloning:
Public Class RefClass
Public tcp As TcpClient
Public name As String
End Class
Public Class RefClassList
Inherits List(Of RefClass)
Implements ICloneable

Public Function Clone() As Object Implements System.ICloneab le.Clone
Return MemberwiseClone ()
End Function

Public Sub RemoveItem(ByVa l item As RefClass)
Dim newList As RefClassList = Me.Clone
newList.Remove( item)
End Sub
End Class

Until now, MemberwiseClone () has served my purposes well. Because RefClass
contains a TcpClient, a deep clone is not desirable, because a Tcp Connection
is not duplicatable (logically). I need to clone because I need to modify the
collection while in a For Each loop; so creating a clone beforehand works for
me. When adding elements to the clone, I have no problems. However, when I
remove an object from the cloned list by referencem using RemoveItem(), the
associated item in the original list is set to Nothing and moved to the end
of the list. ReferenceEquals () applied to the original list and the clone
returns false, and the object has not been deleted (I added it to a separate
class, and it remained unchanged). I would like to know if this is a side
effect of MemberwiseClone , or perhaps Remove, and how I should fix it. I
tried several things, and one option that did not show this effect was:

Public Function Clone() As Object Implements System.ICloneab le.Clone
Dim newList As New UserList
newList.AddRang e(Me.GetRange(0 , Me.Count))
Return newList
End Function

I would like to know if there is a better way to work around this problem,
and I am also curious as to exactly why this happens. I was under the
impression that the member array of a list created through MemberwiseClone ()
would not be linked to the original member list, but the references in the
new list would be the same.
Thanks!
Nov 25 '07 #1
7 2843
I think that what is going on is that the only 'member' of the RefClassList
is the list object (that you inherit), hence the newlist, simply points to
the original list, since that is the way that reference types are cloned.
There are other ways (as you have shown) to copy the emebers of the original
list to a new list. The doc's for memberwise clone of thge generic list
object say ..."Creates a shallow copy of the current Object. (inherited from
Object)". No special override mentioned. So cloning a list object is
exactly the same as setting another reference to the original list. My
thoughts anyhow. You should be able to verify this by cloning one of your
objects and checking if the two underlying lists are the same reference.
--
Terry
"Joel Merk" wrote:
I have created a custom class with both value type members and reference type
members. I then have another custom class which inherits from a generic list
of my first class. This custom listneeds to support cloning:
Public Class RefClass
Public tcp As TcpClient
Public name As String
End Class
Public Class RefClassList
Inherits List(Of RefClass)
Implements ICloneable

Public Function Clone() As Object Implements System.ICloneab le.Clone
Return MemberwiseClone ()
End Function

Public Sub RemoveItem(ByVa l item As RefClass)
Dim newList As RefClassList = Me.Clone
newList.Remove( item)
End Sub
End Class

Until now, MemberwiseClone () has served my purposes well. Because RefClass
contains a TcpClient, a deep clone is not desirable, because a Tcp Connection
is not duplicatable (logically). I need to clone because I need to modify the
collection while in a For Each loop; so creating a clone beforehand works for
me. When adding elements to the clone, I have no problems. However, when I
remove an object from the cloned list by referencem using RemoveItem(), the
associated item in the original list is set to Nothing and moved to the end
of the list. ReferenceEquals () applied to the original list and the clone
returns false, and the object has not been deleted (I added it to a separate
class, and it remained unchanged). I would like to know if this is a side
effect of MemberwiseClone , or perhaps Remove, and how I should fix it. I
tried several things, and one option that did not show this effect was:

Public Function Clone() As Object Implements System.ICloneab le.Clone
Dim newList As New UserList
newList.AddRang e(Me.GetRange(0 , Me.Count))
Return newList
End Function

I would like to know if there is a better way to work around this problem,
and I am also curious as to exactly why this happens. I was under the
impression that the member array of a list created through MemberwiseClone ()
would not be linked to the original member list, but the references in the
new list would be the same.
Thanks!
Nov 25 '07 #2
Well, I was sure wrong about that...should of tried it first. Works fine for
me, both adding and removing. Neither operation on one list affects the
other list for me. Not sure what your removeitem is suppose to do, certainly
wont affect the instance that it is invoked on. Don't think you want to
clone another list there.

--
Terry
"Joel Merk" wrote:
I have created a custom class with both value type members and reference type
members. I then have another custom class which inherits from a generic list
of my first class. This custom listneeds to support cloning:
Public Class RefClass
Public tcp As TcpClient
Public name As String
End Class
Public Class RefClassList
Inherits List(Of RefClass)
Implements ICloneable

Public Function Clone() As Object Implements System.ICloneab le.Clone
Return MemberwiseClone ()
End Function

Public Sub RemoveItem(ByVa l item As RefClass)
Dim newList As RefClassList = Me.Clone
newList.Remove( item)
End Sub
End Class

Until now, MemberwiseClone () has served my purposes well. Because RefClass
contains a TcpClient, a deep clone is not desirable, because a Tcp Connection
is not duplicatable (logically). I need to clone because I need to modify the
collection while in a For Each loop; so creating a clone beforehand works for
me. When adding elements to the clone, I have no problems. However, when I
remove an object from the cloned list by referencem using RemoveItem(), the
associated item in the original list is set to Nothing and moved to the end
of the list. ReferenceEquals () applied to the original list and the clone
returns false, and the object has not been deleted (I added it to a separate
class, and it remained unchanged). I would like to know if this is a side
effect of MemberwiseClone , or perhaps Remove, and how I should fix it. I
tried several things, and one option that did not show this effect was:

Public Function Clone() As Object Implements System.ICloneab le.Clone
Dim newList As New UserList
newList.AddRang e(Me.GetRange(0 , Me.Count))
Return newList
End Function

I would like to know if there is a better way to work around this problem,
and I am also curious as to exactly why this happens. I was under the
impression that the member array of a list created through MemberwiseClone ()
would not be linked to the original member list, but the references in the
new list would be the same.
Thanks!
Nov 25 '07 #3
I have a function that sends a message to the TcpClient of each RefClass
using a for loop. I need to use the close, because sometimes I want to omit
one or many instances of RefClass in my list, and rather than using two For
Each loops, I thought it would be more efficient to to create a clone array,
and remove the members to whom I do not wish to send a message. After this
operation, the clone would be discarded. The examples I have provided are
extremely simplified; I am just using them as an example.
As a contrast, there are several other ways of copying the list members that
still observe my problem. For example:

Public Function Clone() As Object Implements System.ICloneab le.Clone
Dim newList As New RefClassList
newList.AddRang e(Me)
Return newList
End Function

I guess what I'm trying to understand, is if the two lists are indeed
references to the same object, then why, when removing an item from a clone
(Using Remove or RemoveAt), does the original list's item simply get set to
Nothing and moved to the end instead of completely removed. If you want to
get the same effect as me, what I did is created a new list, added several
items with incremental names, created a clone, and removed one of the middle
items using RemoveAt().

Something else I would now like to understand is why or how a clone would
affect a list this way. When Memberwiseclone is applied, is a new
RefClassList created, with an idental reference to the default property?

I guess I will have to experiment with different workarounds and choose the
one that I like best, unless someone has a recommendation?

Thanks for the reply!

"Terry" wrote:
Well, I was sure wrong about that...should of tried it first. Works fine for
me, both adding and removing. Neither operation on one list affects the
other list for me. Not sure what your removeitem is suppose to do, certainly
wont affect the instance that it is invoked on. Don't think you want to
clone another list there.

--
Terry
Nov 25 '07 #4
Hi Joel,
What I was trying to say, is that I can't duplicate your problem. I
created a simple console app and don't seem to have the same trouble you are
talking about. BTW, I had to turn Option strict off to compile your code.
And to make the RemoveItem work, got rid of the newlist - that was also what
I was saying, I don't understand what your removeitem was suppose to do. It
created a brand new clone, removed the item and then the brand new clone is
discarded. What's the purpose? I assume that the code is suppose to remove
the item from itself, so that is what I made it do. The 2 lists seem totally
independent to me. Here is the code I tested it with.

Option Strict Off

Module Module1

Sub Main()
Dim rc As New RefClass
Dim rc2 As New RefClass
Dim rcl1 As New RefClassList
rcl1.Add(rc)
rcl1.Add(rc2)
Dim rcl2 As RefClassList = rcl1.Clone
rcl2.RemoveItem (rc)
Console.WriteLi ne(rcl1.Count)
Console.WriteLi ne(rcl2.Count)
Console.ReadLin e()
End Sub

End Module
Public Class RefClass
Public tcp As Object
Public name As String
End Class
Public Class RefClassList
Inherits List(Of RefClass)
Implements ICloneable

Public Function Clone() As Object Implements System.ICloneab le.Clone
Return MemberwiseClone ()
End Function

Public Sub RemoveItem(ByVa l item As RefClass)
'Dim newList As RefClassList = Me.Clone
Remove(item)
End Sub
End Class

--
Terry
"Joel Merk" wrote:
I have a function that sends a message to the TcpClient of each RefClass
using a for loop. I need to use the close, because sometimes I want to omit
one or many instances of RefClass in my list, and rather than using two For
Each loops, I thought it would be more efficient to to create a clone array,
and remove the members to whom I do not wish to send a message. After this
operation, the clone would be discarded. The examples I have provided are
extremely simplified; I am just using them as an example.
As a contrast, there are several other ways of copying the list members that
still observe my problem. For example:

Public Function Clone() As Object Implements System.ICloneab le.Clone
Dim newList As New RefClassList
newList.AddRang e(Me)
Return newList
End Function

I guess what I'm trying to understand, is if the two lists are indeed
references to the same object, then why, when removing an item from a clone
(Using Remove or RemoveAt), does the original list's item simply get set to
Nothing and moved to the end instead of completely removed. If you want to
get the same effect as me, what I did is created a new list, added several
items with incremental names, created a clone, and removed one of the middle
items using RemoveAt().

Something else I would now like to understand is why or how a clone would
affect a list this way. When Memberwiseclone is applied, is a new
RefClassList created, with an idental reference to the default property?

I guess I will have to experiment with different workarounds and choose the
one that I like best, unless someone has a recommendation?

Thanks for the reply!

"Terry" wrote:
Well, I was sure wrong about that...should of tried it first. Works fine for
me, both adding and removing. Neither operation on one list affects the
other list for me. Not sure what your removeitem is suppose to do, certainly
wont affect the instance that it is invoked on. Don't think you want to
clone another list there.

--
Terry
Nov 26 '07 #5
Thanks again for the help, Terry!
I tested your code, and you are indeed experiencing the same problem as I,
but you are not looking it in the right way. The count of the original array
remains the same, but the item that was removed from the clone becomes
nothing and is moved to the end. If you were to use the code:

Sub Main()
Dim rc As New RefClass
Dim rc2 As New RefClass
Dim rcl1 As New RefClassList
rcl1.Add(rc)
rcl1.Add(rc2)
Dim rcl2 As RefClassList = rcl1.Clone
rcl2.RemoveItem (rc)
Console.WriteLi ne(rcl1(0).ToSt ring)
Console.WriteLi ne(rcl1(1).ToSt ring)
Console.WriteLi ne(rcl2(0).ToSt ring)
Console.ReadLin e()
End Sub

as your sub main, you would encounter a NullReferenceEx ception at
Console.WriteLi ne(rcl1(1).ToSt ring)
because rcl1(1) is a reference to Nothing, even though rcl1 still has 2
items. This is the problem I am having. Also, the reason that my RemoveItem
appeared to do nothing is because I felt that whatever else it would need to
do was not important; what I was actually going to do is perform a loop
operation on the new modified (smaller) clone before discarding it, but this
is not actually relevant to the problem that I am having.
Thanks!

"Terry" wrote:
Hi Joel,
What I was trying to say, is that I can't duplicate your problem. I
created a simple console app and don't seem to have the same trouble you are
talking about. BTW, I had to turn Option strict off to compile your code.
And to make the RemoveItem work, got rid of the newlist - that was also what
I was saying, I don't understand what your removeitem was suppose to do. It
created a brand new clone, removed the item and then the brand new clone is
discarded. What's the purpose? I assume that the code is suppose to remove
the item from itself, so that is what I made it do. The 2 lists seem totally
independent to me. Here is the code I tested it with.

Option Strict Off

Module Module1

Sub Main()
Dim rc As New RefClass
Dim rc2 As New RefClass
Dim rcl1 As New RefClassList
rcl1.Add(rc)
rcl1.Add(rc2)
Dim rcl2 As RefClassList = rcl1.Clone
rcl2.RemoveItem (rc)
Console.WriteLi ne(rcl1.Count)
Console.WriteLi ne(rcl2.Count)
Console.ReadLin e()
End Sub

End Module
Public Class RefClass
Public tcp As Object
Public name As String
End Class
Public Class RefClassList
Inherits List(Of RefClass)
Implements ICloneable

Public Function Clone() As Object Implements System.ICloneab le.Clone
Return MemberwiseClone ()
End Function

Public Sub RemoveItem(ByVa l item As RefClass)
'Dim newList As RefClassList = Me.Clone
Remove(item)
End Sub
End Class

--
Terry
"Joel Merk" wrote:
I have a function that sends a message to the TcpClient of each RefClass
using a for loop. I need to use the close, because sometimes I want to omit
one or many instances of RefClass in my list, and rather than using two For
Each loops, I thought it would be more efficient to to create a clone array,
and remove the members to whom I do not wish to send a message. After this
operation, the clone would be discarded. The examples I have provided are
extremely simplified; I am just using them as an example.
As a contrast, there are several other ways of copying the list members that
still observe my problem. For example:

Public Function Clone() As Object Implements System.ICloneab le.Clone
Dim newList As New RefClassList
newList.AddRang e(Me)
Return newList
End Function

I guess what I'm trying to understand, is if the two lists are indeed
references to the same object, then why, when removing an item from a clone
(Using Remove or RemoveAt), does the original list's item simply get set to
Nothing and moved to the end instead of completely removed. If you want to
get the same effect as me, what I did is created a new list, added several
items with incremental names, created a clone, and removed one of the middle
items using RemoveAt().

Something else I would now like to understand is why or how a clone would
affect a list this way. When Memberwiseclone is applied, is a new
RefClassList created, with an idental reference to the default property?

I guess I will have to experiment with different workarounds and choose the
one that I like best, unless someone has a recommendation?

Thanks for the reply!

"Terry" wrote:
Well, I was sure wrong about that...should of tried it first. Works fine for
me, both adding and removing. Neither operation on one list affects the
other list for me. Not sure what your removeitem is suppose to do, certainly
wont affect the instance that it is invoked on. Don't think you want to
clone another list there.
>
--
Terry
Nov 28 '07 #6
Ok - Now I understand. Now my first answer is looking better! I am still
in the process of learning all this stuff myself, and this is one of the ways
that I use to learn. I really think the first answer I gave had us going in
the right direction. When you inherit from the list type, you become a list.
I would guess that inside the list type somewhere there is a reference
(type) that points to the actual data (maybe an array, not sure). A shallow
copy will just point to this same data structure. So now, you have 2 list
objects that maintain their own count, but both point to the same underlying
data structure. When you update that structure through one of the list
objects, the other does not know about it! So it is not that removing an
item from the clone causes the original to move the item to the end of the
list, it is the item was removed from both underlying structures, but the
original does not know that the list is one shorter now! The item itself is
not nothing. It is that the list is one shorter! Here is the console app
one more time. Are you an MSDN subscriber? The reason I ask, is that I am
surprised no ms person has jumped in here. If not, maybe I can reask the
question under my name and we can get an expert to explain it ot us.
Any how, here is the code that I think shows a little better what is
happening.
Module Module1
Sub Main()
Dim rc1 As New RefClass
Dim rc2 As New RefClass
Dim rcl1 As New RefClassList
rcl1.Add(rc1)
rcl1.Add(rc2)
rcl1(0).name = "One"
rcl1(1).name = "Two"
Dim rcl2 As RefClassList = DirectCast(rcl1 .Clone, RefClassList)
rcl2.Remove(rc1 )
Console.WriteLi ne(rcl1.Count) ' 2
Console.WriteLi ne(rcl2.Count) ' 1
Console.WriteLi ne(rcl2(0).name ) ' Two - now the first (only)
Console.WriteLi ne(rcl1(0).name ) 'Two!!!!!!!!!!! !!
Console.WriteLi ne(rc1.name) 'One - it still exists!
Console.WriteLi ne(rcl1(1).name ) 'Null Reference exception!!!!!!
Console.ReadLin e()
End Sub
End Module
Public Class RefClass
Public name As String
End Class
Public Class RefClassList
Inherits List(Of RefClass)
Implements ICloneable

Public Function Clone() As Object Implements System.ICloneab le.Clone
Return MemberwiseClone ()
End Function
End Class

--
Terry
"Joel Merk" wrote:
Thanks again for the help, Terry!
I tested your code, and you are indeed experiencing the same problem as I,
but you are not looking it in the right way. The count of the original array
remains the same, but the item that was removed from the clone becomes
nothing and is moved to the end. If you were to use the code:

Sub Main()
Dim rc As New RefClass
Dim rc2 As New RefClass
Dim rcl1 As New RefClassList
rcl1.Add(rc)
rcl1.Add(rc2)
Dim rcl2 As RefClassList = rcl1.Clone
rcl2.RemoveItem (rc)
Console.WriteLi ne(rcl1(0).ToSt ring)
Console.WriteLi ne(rcl1(1).ToSt ring)
Console.WriteLi ne(rcl2(0).ToSt ring)
Console.ReadLin e()
End Sub

as your sub main, you would encounter a NullReferenceEx ception at
Console.WriteLi ne(rcl1(1).ToSt ring)
because rcl1(1) is a reference to Nothing, even though rcl1 still has 2
items. This is the problem I am having. Also, the reason that my RemoveItem
appeared to do nothing is because I felt that whatever else it would need to
do was not important; what I was actually going to do is perform a loop
operation on the new modified (smaller) clone before discarding it, but this
is not actually relevant to the problem that I am having.
Thanks!

"Terry" wrote:
Hi Joel,
What I was trying to say, is that I can't duplicate your problem. I
created a simple console app and don't seem to have the same trouble you are
talking about. BTW, I had to turn Option strict off to compile your code.
And to make the RemoveItem work, got rid of the newlist - that was also what
I was saying, I don't understand what your removeitem was suppose to do. It
created a brand new clone, removed the item and then the brand new clone is
discarded. What's the purpose? I assume that the code is suppose to remove
the item from itself, so that is what I made it do. The 2 lists seem totally
independent to me. Here is the code I tested it with.

Option Strict Off

Module Module1

Sub Main()
Dim rc As New RefClass
Dim rc2 As New RefClass
Dim rcl1 As New RefClassList
rcl1.Add(rc)
rcl1.Add(rc2)
Dim rcl2 As RefClassList = rcl1.Clone
rcl2.RemoveItem (rc)
Console.WriteLi ne(rcl1.Count)
Console.WriteLi ne(rcl2.Count)
Console.ReadLin e()
End Sub

End Module
Public Class RefClass
Public tcp As Object
Public name As String
End Class
Public Class RefClassList
Inherits List(Of RefClass)
Implements ICloneable

Public Function Clone() As Object Implements System.ICloneab le.Clone
Return MemberwiseClone ()
End Function

Public Sub RemoveItem(ByVa l item As RefClass)
'Dim newList As RefClassList = Me.Clone
Remove(item)
End Sub
End Class

--
Terry
"Joel Merk" wrote:
I have a function that sends a message to the TcpClient of each RefClass
using a for loop. I need to use the close, because sometimes I want to omit
one or many instances of RefClass in my list, and rather than using two For
Each loops, I thought it would be more efficient to to create a clone array,
and remove the members to whom I do not wish to send a message. After this
operation, the clone would be discarded. The examples I have provided are
extremely simplified; I am just using them as an example.
As a contrast, there are several other ways of copying the list members that
still observe my problem. For example:
>
Public Function Clone() As Object Implements System.ICloneab le.Clone
Dim newList As New RefClassList
newList.AddRang e(Me)
Return newList
End Function
>
I guess what I'm trying to understand, is if the two lists are indeed
references to the same object, then why, when removing an item from a clone
(Using Remove or RemoveAt), does the original list's item simply get set to
Nothing and moved to the end instead of completely removed. If you want to
get the same effect as me, what I did is created a new list, added several
items with incremental names, created a clone, and removed one of the middle
items using RemoveAt().
>
Something else I would now like to understand is why or how a clone would
affect a list this way. When Memberwiseclone is applied, is a new
RefClassList created, with an idental reference to the default property?
>
I guess I will have to experiment with different workarounds and choose the
one that I like best, unless someone has a recommendation?
>
Thanks for the reply!
>
"Terry" wrote:
>
Well, I was sure wrong about that...should of tried it first. Works fine for
me, both adding and removing. Neither operation on one list affects the
other list for me. Not sure what your removeitem is suppose to do, certainly
wont affect the instance that it is invoked on. Don't think you want to
clone another list there.

--
Terry
Nov 28 '07 #7
Ok, think I have thought of a better(?) more general way to look at this.
Whenever you clone (shallow) a 'wrapper' type class, you end up with 2
wrappers wrapping the same object!
I *think* that this is a correct statement. And I *think* that a List is a
wrapper around something (probably just an array).
--
Terry
"Joel Merk" wrote:
Thanks again for the help, Terry!
I tested your code, and you are indeed experiencing the same problem as I,
but you are not looking it in the right way. The count of the original array
remains the same, but the item that was removed from the clone becomes
nothing and is moved to the end. If you were to use the code:

Sub Main()
Dim rc As New RefClass
Dim rc2 As New RefClass
Dim rcl1 As New RefClassList
rcl1.Add(rc)
rcl1.Add(rc2)
Dim rcl2 As RefClassList = rcl1.Clone
rcl2.RemoveItem (rc)
Console.WriteLi ne(rcl1(0).ToSt ring)
Console.WriteLi ne(rcl1(1).ToSt ring)
Console.WriteLi ne(rcl2(0).ToSt ring)
Console.ReadLin e()
End Sub

as your sub main, you would encounter a NullReferenceEx ception at
Console.WriteLi ne(rcl1(1).ToSt ring)
because rcl1(1) is a reference to Nothing, even though rcl1 still has 2
items. This is the problem I am having. Also, the reason that my RemoveItem
appeared to do nothing is because I felt that whatever else it would need to
do was not important; what I was actually going to do is perform a loop
operation on the new modified (smaller) clone before discarding it, but this
is not actually relevant to the problem that I am having.
Thanks!

"Terry" wrote:
Hi Joel,
What I was trying to say, is that I can't duplicate your problem. I
created a simple console app and don't seem to have the same trouble you are
talking about. BTW, I had to turn Option strict off to compile your code.
And to make the RemoveItem work, got rid of the newlist - that was also what
I was saying, I don't understand what your removeitem was suppose to do. It
created a brand new clone, removed the item and then the brand new clone is
discarded. What's the purpose? I assume that the code is suppose to remove
the item from itself, so that is what I made it do. The 2 lists seem totally
independent to me. Here is the code I tested it with.

Option Strict Off

Module Module1

Sub Main()
Dim rc As New RefClass
Dim rc2 As New RefClass
Dim rcl1 As New RefClassList
rcl1.Add(rc)
rcl1.Add(rc2)
Dim rcl2 As RefClassList = rcl1.Clone
rcl2.RemoveItem (rc)
Console.WriteLi ne(rcl1.Count)
Console.WriteLi ne(rcl2.Count)
Console.ReadLin e()
End Sub

End Module
Public Class RefClass
Public tcp As Object
Public name As String
End Class
Public Class RefClassList
Inherits List(Of RefClass)
Implements ICloneable

Public Function Clone() As Object Implements System.ICloneab le.Clone
Return MemberwiseClone ()
End Function

Public Sub RemoveItem(ByVa l item As RefClass)
'Dim newList As RefClassList = Me.Clone
Remove(item)
End Sub
End Class

--
Terry
"Joel Merk" wrote:
I have a function that sends a message to the TcpClient of each RefClass
using a for loop. I need to use the close, because sometimes I want to omit
one or many instances of RefClass in my list, and rather than using two For
Each loops, I thought it would be more efficient to to create a clone array,
and remove the members to whom I do not wish to send a message. After this
operation, the clone would be discarded. The examples I have provided are
extremely simplified; I am just using them as an example.
As a contrast, there are several other ways of copying the list members that
still observe my problem. For example:
>
Public Function Clone() As Object Implements System.ICloneab le.Clone
Dim newList As New RefClassList
newList.AddRang e(Me)
Return newList
End Function
>
I guess what I'm trying to understand, is if the two lists are indeed
references to the same object, then why, when removing an item from a clone
(Using Remove or RemoveAt), does the original list's item simply get set to
Nothing and moved to the end instead of completely removed. If you want to
get the same effect as me, what I did is created a new list, added several
items with incremental names, created a clone, and removed one of the middle
items using RemoveAt().
>
Something else I would now like to understand is why or how a clone would
affect a list this way. When Memberwiseclone is applied, is a new
RefClassList created, with an idental reference to the default property?
>
I guess I will have to experiment with different workarounds and choose the
one that I like best, unless someone has a recommendation?
>
Thanks for the reply!
>
"Terry" wrote:
>
Well, I was sure wrong about that...should of tried it first. Works fine for
me, both adding and removing. Neither operation on one list affects the
other list for me. Not sure what your removeitem is suppose to do, certainly
wont affect the instance that it is invoked on. Don't think you want to
clone another list there.

--
Terry
Nov 28 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1721
by: RMD | last post by:
I need to be able to keep a list of object references, and null them out one by one at a later point in time. I realize this can be dangerous, but I have my reasons. I can't figure out, however, how to keep what is essentially a reference to a reference. I want to store my objects in an ArrayList, then loop through the array list and null out the original reference I was given when I added it to my Array List. Below is some example...
1
21392
by: Junior | last post by:
I keep receiving this "The type or namespace name 'CASsEventHandler' could not be found (are you missing a using directive or an assembly reference?)" message in two particular lines, and I've tried everything... Could anyone please paste this and tell me what I'm doing wrong ? I use SerialPort.zip, which can be downloaded at http://www.gotdotnet.com/Community/UserSamples/Details.aspx?SampleGuid=b06e30f9-1301-4cc6-ac14-dfe325097c69 ...
13
2196
by: ahaupt | last post by:
Hi all, I'm implementing the Clone() method through the ICloneable interface and don't quite know how deep I need to go for a deep copy. Example: class A: ICloneable { object _val;
2
2898
by: rajivpopat | last post by:
I've been reading a discussion thread at http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_frm/thread/119f8362a9f5ff52 regarding typecasting generic collections to classical collections and vice-a-versa I faced a similar problem and solved it slightly differently... The apporach i seems to work but i am sure someone has a better apporach for solving this problem. In my case i was using nHibernate which was...
2
2676
by: Martin Ortiz | last post by:
Ugh.... All classes are copy by reference, even if you use "ByVal" and NOT "ByRef" it's still a copy by reference. Of course, as a consequence, if you change any values of the object you passed in, the original object is affected. I expect this with arrays, but not with single objects being passed "ByVal" to functions.... Is there a sane to get "ByVal" passing of parameters? (copying fields is not
3
2450
by: muchan | last post by:
I'm a C++ programmer now poking into C#. I wanted to write a snippet of code, equivalent of //--- C++ code --------------- #include <string> #include <vector> class obj { // a POD class public: std::string s;
6
6112
by: Niyazi | last post by:
Hi all, What is fastest way removing duplicated value from string array using vb.net? Here is what currently I am doing but the the array contains over 16000 items. And it just do it in 10 or more minutes. 'REMOVE DUBLICATED VALUE FROM ARRAY +++++++++++++++++ Dim col As New Scripting.Dictionary Dim ii As Integer = 0
5
11360
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget to pass all the necessary object files to the linker. Neither of those are my problem. Please bear with me as the question I ask is rather long and I think it's beyond a CS101 level of linker stupidity. If it is a stupid CS101 mistake I'm making...
15
2091
by: mark.norgate | last post by:
Hello I want to create a reference to an object, so that changes to the referenced object are reflected in the other object. Like this: object o = 123; object p = o; o = 456;
0
9563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9997
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9937
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8821
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.