I have a List of structure’s in my VB.NET program, and I’m looping over them, and change the values of the objects in the list, as follows
Dim retvals As List(Of SomeStruct) = parser.RetrieveData(new_path)
For i As Integer = 0 To retvals.Count - 1 Step 1
dim temp as SomeStruct = retvals(i)
temp.A = GetValueForA()
temp.B = GetValueForB()
Next
When I look into my List of structs after this loop, none of the values were overwritten. Why? I thought that I had references in my list, so if I change reference A to a struct, then reference B to the same struct should see the changes?
What am I missing?
I realized what the problem was, the
Structurein .NET is a value type, meaning the contents will get copied into theList. So changing thetempvariable will not change the original in theListI fixed it by using a
classinstead, which is a reference type.