Z
zacks
I have written a serialized class that has several properties that are
typed as a list of type class. When I deserialize an XML file, the list
is populated just fine. But I am having trouble manually loading the
class for serialization when the class has not been initialized by a
deserialize. If I add three instances of a class to the list, each with
different values for their properties, the XML file created by
serialization has three items for that property, but the values in all
three are the same values, the values of the last class added to the
list. I have tried a test of this that merely loads up a list of
classes in a class that is not being serialized and dump the property
values with the same result. Any ideas?
Sample code:
My custom class to be listed:
Public Class clsItem
Private _Part1 As String
Private _Part2 As String
Public Property Part1() As String
Get
Return _Part1
End Get
Set(ByVal value As String)
_part2 = value
End Set
End Property
Public Property Part2() As String
Get
Return _Part2
End Get
Set(ByVal value As String)
_Part2 = value
End Set
End Property
End Class
Code to load up the list and dump it:
Dim myItem As New clsItem
Dim myItems As New List(Of clsItem)
Dim i As Integer = 1
myItem.Part1 = "This is Part 1 the First Item in the List"
myItem.Part2 = "This is Part 2 the First Item in the List"
myItems.Add(myItem)
myItem.Part1 = "This is Part 1 the Second Item in the List"
myItem.Part2 = "This is Part 2 the Second Item in the List"
myItems.Add(myItem)
myItem.Part1 = "This is Part 1 the Third Item in the List"
myItem.Part2 = "This is Part 2 the Third Item in the List"
myItems.Add(myItem)
For Each myItem In myItems
Debug.WriteLine("Item Number " & CStr(i))
Debug.WriteLine("Part1 = " & myItem.Part1)
Debug.WriteLine("Part2 = " & myItem.Part2)
i += 1
Next
The output:
Item Number 1
Part1 =
Part2 = This is Part 2 the Third Item in the List
Item Number 2
Part1 =
Part2 = This is Part 2 the Third Item in the List
Item Number 3
Part1 =
Part2 = This is Part 2 the Third Item in the List
Notice that the first property called Part1 never gets any value.
typed as a list of type class. When I deserialize an XML file, the list
is populated just fine. But I am having trouble manually loading the
class for serialization when the class has not been initialized by a
deserialize. If I add three instances of a class to the list, each with
different values for their properties, the XML file created by
serialization has three items for that property, but the values in all
three are the same values, the values of the last class added to the
list. I have tried a test of this that merely loads up a list of
classes in a class that is not being serialized and dump the property
values with the same result. Any ideas?
Sample code:
My custom class to be listed:
Public Class clsItem
Private _Part1 As String
Private _Part2 As String
Public Property Part1() As String
Get
Return _Part1
End Get
Set(ByVal value As String)
_part2 = value
End Set
End Property
Public Property Part2() As String
Get
Return _Part2
End Get
Set(ByVal value As String)
_Part2 = value
End Set
End Property
End Class
Code to load up the list and dump it:
Dim myItem As New clsItem
Dim myItems As New List(Of clsItem)
Dim i As Integer = 1
myItem.Part1 = "This is Part 1 the First Item in the List"
myItem.Part2 = "This is Part 2 the First Item in the List"
myItems.Add(myItem)
myItem.Part1 = "This is Part 1 the Second Item in the List"
myItem.Part2 = "This is Part 2 the Second Item in the List"
myItems.Add(myItem)
myItem.Part1 = "This is Part 1 the Third Item in the List"
myItem.Part2 = "This is Part 2 the Third Item in the List"
myItems.Add(myItem)
For Each myItem In myItems
Debug.WriteLine("Item Number " & CStr(i))
Debug.WriteLine("Part1 = " & myItem.Part1)
Debug.WriteLine("Part2 = " & myItem.Part2)
i += 1
Next
The output:
Item Number 1
Part1 =
Part2 = This is Part 2 the Third Item in the List
Item Number 2
Part1 =
Part2 = This is Part 2 the Third Item in the List
Item Number 3
Part1 =
Part2 = This is Part 2 the Third Item in the List
Notice that the first property called Part1 never gets any value.