|
-
Nov 14th, 2008, 03:29 PM
#1
Thread Starter
Addicted Member
Issue with Lists
I have 2 lists, SortedValuesA and SortedValuesB.
Heres the code I have a problem with.....
VB Code:
Dim SortedValuesb As New List(Of String())
Dim SortedValuesa As New List(Of String())
.
.
.
'I assign a 28 arrays to List SortedValuesa
'Pull some out of A and add to B
.
.
.
SortedValuesa = SortedValuesb
MsgBox(SortedValuesa.Count)
SortedValuesb.Clear()
MsgBox(SortedValuesa.Count)
Now when this runs, the fist MSGBOX gives me 18, which is correct. Now the second MSGBOX gives me 0.... Thats my problem.
By saying A=B they are now linked? So clearing B will clear A? How do I fix this?
I'm using B as a buffer to hold temporary values pulled from A. The next step beyond this code is to do a few more layers of "stripping" until I have the final arrays that I need.
-
Nov 14th, 2008, 04:47 PM
#2
Re: Issue with Lists
The List(Of T), like all other classes, are reference types. That means that your "SortedValuesa" and "SortedValuesb" are two variables that hold references to class instances in memory. When you assign SortedValuesb to SortedValuesa, you're actually making SortedValuesa refer to the same class instance in memory as SortedValuesb. It doesnt matter if you call the Clear method on SortedValuesa or SortedValuesb, as they refer to the same list.
If you want to create a new List that is a copy of another one, use the constructor overload that allows it:
VB.NET Code:
Dim listOne As New List(Of Integer)
listOne.Add(5)
listOne.Add(55)
listOne.Add(102)
Dim listTwo As New List(Of Integer)(listOne)
listOne.Clear()
MessageBox.Show(listTwo.Count.ToString())
-
Nov 14th, 2008, 08:22 PM
#3
Frenzied Member
Re: Issue with Lists
Oh...Very nice explanation...Clears up a lot...
Please rate helpful ppl's posts. It's the best 'thank you' you can give 
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
|