Weird behaviour of System.Collections.Generic to create lists
I’m usingSystem.Collections.Generic to create lists (which contain other lists) in a Visual Studio 2005 (updated with SP1) application when I ran across this strange behaviour.I distilled the problem down to the minimal code below to illustrate the issue.Am I missing something or is this a possible compiler bug?
I have a simple form with a button on it the contains the following handler:
PublicClass Form1
PrivateSub Button1_Click(ByVal senderAs System.Object,ByVal eAs System.EventArgs)Handles Button1.Click
Dim this_listAsNew My_list
this_list.Fill_string_values(this_list.indexed_frame_data(0),"list_A", 0,"A0")
this_list.Fill_string_values(this_list.indexed_frame_data(0),"list_B", 0,"B0")
this_list.Fill_string_values(this_list.indexed_frame_data(0),"list_B", 1,"B1")
this_list.Fill_string_values(this_list.indexed_frame_data(0),"list_B", 2,"B2")
EndSub
EndClass
A separate file contain the class (See below) that handles the list.
Imports System.Collections.Generic
PublicClass My_list
PublicStructure frame_values
Dim list_AAs List(OfString), list_BAs List(OfString)
EndStructure
Private My_data_listAsNew List(Of frame_values)
Property indexed_frame_data(ByVal indexAsInteger)As frame_values
Get
If My_data_list.Count <= indexThen
Dim newFrameAsNew frame_values'this line (and the code until the end if) is only executed once (as expected)
'when the "0 'th" item is created for My_data_list
Dim default_Point_list_aAsNew List(OfString)
Dim default_Point_list_bAsNew List(OfString)
default_Point_list_a.Add("Empty")
default_Point_list_b.Add("Empty")
newFrame.list_A = default_Point_list_a'add a default value to both "list_A" and "list_B" to make sure we have at least one value
newFrame.list_B = default_Point_list_a
'newFrame.list_B = default_Point_list_b'UN-COMMENT this line then routines work as expected
My_data_list.Add(newFrame)
EndIf
Return My_data_list(index)
EndGet
Set(ByVal valueAs frame_values)
My_data_list(index) = value
EndSet
EndProperty
Sub Fill_string_values(ByRef selected_frameAs frame_values,ByVal structure_member_selectorAsString,ByVal start_indexAsInteger,ByVal string_dataAsString)
Dim Selected_memberAs List(OfString)
SelectCase (structure_member_selector)
Case"list_A"
Selected_member = selected_frame.list_A
Case"list_B"
Selected_member = selected_frame.list_B
CaseElse
Selected_member =Nothing
EndSelect
If (Selected_member.Count <= start_index)Then
Selected_member.Add("")
EndIf
Selected_member(start_index) = string_data
EndSub
EndClass
The list should contain :
frame_values(0).list_A=A0
frame_values(0).list_B=B0
frame_values(0).list_B=B1
frame_values(0).list_B=B2
Instead as the calls are made,list_A is modified along with list_B
So it ends up with:
frame_values(0).list_A=B0
frame_values(0).list_A=B1
frame_values(0).list_A=B2
frame_values(0).list_B=B0
frame_values(0).list_B=B1
frame_values(0).list_B=B2
If I uncomment the line in the “PublicClass My_list” code (see the code above for the line “'newFrame.list_B = default_Point_list_b'UN-COMMENT this line then routines work as expected”
Then the code works as expected.Am I doing something wrong?Any help would be appreciated.
Magyary

