Results 1 to 10 of 10

Thread: Serialize a collection of objects

Threaded View

  1. #1

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Serialize a collection of objects

    I'm trying to serialize a collection of a certain type of class...

    I keep getting a Reflection error when I attempt to serialize with the code below... with the two classes below. I can sucessfully serialze the SalesRep class, but not its collection class... I CAN SUCESSFULLY serialize it to a BinaryFormatter ...and retrieve.. but not when using XMLSerializer...

    VB Code:
    1. Dim gwhiz As Xml.Serialization.XmlSerializer = New Xml.Serialization.XmlSerializer(GetType(SalesRepCollection))
    2.         Dim rep1 As SalesRep = New SalesRep(12, "John", "Labrizzi")
    3.         Dim rep2 As SalesRep = New SalesRep(5, "Eddie", "Gasser")
    4.         Dim myReps As SalesRepCollection = New SalesRepCollection()
    5.         myReps.Add(rep1)
    6.         myReps.Add(rep2)
    7.  
    8.        dim fs as System.IO.FileStream = New System.IO.FileStream("C:\sales.xml", IO.FileMode.Create)
    9.         gwhiz.Serialize(fs, myReps)
    10.         fs.Close()

    Here is the SalesRep Class:
    VB Code:
    1. <Serializable()> Public Class SalesRep
    2.  
    3.     Public Sub New()
    4.  
    5.     End Sub
    6.  
    7.     Public Sub New(ByVal RepID As Int32, ByVal RepFirstName As String, _
    8.     ByVal RepLastName As String, Optional ByVal RepMiddleName As String = Nothing)
    9.  
    10.         sRepID = RepID
    11.         sRepFName = RepFirstName
    12.         sRepMName = RepMiddleName
    13.         sRepLName = RepLastName
    14.     End Sub
    15.  
    16.     Private sRepID As Int32
    17.     Private sRepFName As String
    18.     Private sRepMName As String
    19.     Private sRepLName As String
    20.  
    21.     Public Property SalesRepId() As Int32
    22.         Get
    23.             Return sRepID
    24.         End Get
    25.         Set(ByVal Value As Int32)
    26.             sRepID = Value
    27.         End Set
    28.     End Property
    29.     Public Property FirstName() As String
    30.         Get
    31.             Return sRepFName
    32.         End Get
    33.         Set(ByVal Value As String)
    34.             sRepFName = Value
    35.         End Set
    36.     End Property
    37.     Public Property MiddleName() As String
    38.         Get
    39.             Return sRepMName
    40.         End Get
    41.         Set(ByVal Value As String)
    42.             sRepMName = Value
    43.         End Set
    44.     End Property
    45.     Public Property LastName() As String
    46.         Get
    47.             Return sRepLName
    48.         End Get
    49.         Set(ByVal Value As String)
    50.             sRepLName = Value
    51.         End Set
    52.     End Property

    Here's the SalesRep collection class:
    VB Code:
    1. Option Strict Off
    2. <Serializable()> Public Class SalesRepCollection
    3.     Inherits System.Collections.CollectionBase
    4.  
    5.  
    6.     Public Function Add(ByVal Salesman As SalesRep) As Integer
    7.         ' Invokes Add method of the List object to add a object.
    8.         Return List.Add(Salesman)
    9.  
    10.     End Function
    11.     Public Sub New()
    12.  
    13.     End Sub
    14.  
    15.     Public Sub Remove(ByVal index As Integer)
    16.  
    17.         If index > Count - 1 Or index < 0 Then
    18.  
    19.         Else
    20.             ' Invokes the RemoveAt method of the List object.
    21.             List.RemoveAt(index)
    22.         End If
    23.     End Sub
    24.  
    25.     '
    26.     Public Function GetItem(ByVal index As Integer) As SalesRep
    27.         Return CType(List.Item(index), SalesRep)
    28.     End Function
    29.  
    30.    
    31.     Public Sub SetItem(ByVal index As Integer, ByVal SalesMan As SalesRep)
    32.  
    33.         List.Item(index) = CType(SalesMan, SalesRep)
    34.     End Sub
    Last edited by nemaroller; Apr 24th, 2003 at 02:44 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width