Results 1 to 4 of 4

Thread: Custom Collection Class Serialization [Resolved]

  1. #1

    Thread Starter
    Hyperactive Member ZeroCool's Avatar
    Join Date
    Feb 2002
    Location
    In front of my computer
    Posts
    423

    Custom Collection Class Serialization [Resolved]

    I created a serializable collection class (ErrorCollection) and after everything was working. i added a few features. went to test the new features and everything just hanged. if i didnt Call ErrorCollection.SerialzeObject i worked fine (apart from creating the xml file)

    I Think i removed everything i added but it still hangs. meh....

    VB Code:
    1. Option Explicit On
    2.  
    3. Imports System.IO
    4. Imports System.Xml.Serialization
    5.  
    6. Public Class ErrorCollection
    7.     Inherits System.Collections.CollectionBase
    8.  
    9. #Region " Collection Methods "
    10.     Public Overridable Function Add(ByVal value As ErrorCode) As Integer
    11.         MyBase.List.Add(value)
    12.     End Function
    13.  
    14.     Default Public Overridable Property Item(ByVal index As Integer) As ErrorCode
    15.         Get
    16.             Return DirectCast(MyBase.List.Item(index), ErrorCode)
    17.         End Get
    18.         Set(ByVal value As ErrorCode)
    19.             MyBase.List.Item(index) = value
    20.         End Set
    21.     End Property
    22.  
    23.     Public Overridable Sub Remove(ByVal index As Integer)
    24.         Me.List.RemoveAt(index)
    25.     End Sub
    26. #End Region
    27.  
    28. #Region " Serialization Methods "
    29.  
    30.     Public Shared Sub SerializeObject(ByVal FileName As String, ByVal CollectionInstance As ErrorCollection)
    31.  
    32.         'Try
    33.  
    34.         Dim root As New XmlRootAttribute
    35.  
    36.         root.Namespace = ""
    37.         root.ElementName = "ErrorCodes"
    38.  
    39.         Dim Namespaces As New XmlSerializerNamespaces
    40.  
    41.         Namespaces.Add("", "")
    42.  
    43.         'Create a new XmlSerialzer instance.
    44.         Dim s As New XmlSerializer(GetType(ErrorCollection), root)
    45.  
    46.         'Writeing the XML file to disk requires a TextWriter
    47.         Dim sw As New StreamWriter(FileName)
    48.  
    49.         ' Serialize the object, and close the StreamWriter.
    50.  
    51.         s.Serialize(sw, CollectionInstance, Namespaces)
    52.  
    53.         sw.Close()
    54.         'Catch ex As Exception
    55.         '    Throw New Exception("Could not Serialize Object")
    56.         'End Try
    57.     End Sub
    58.  
    59.     Public Shared Function DeserializeObject(ByVal FileName As String) As ErrorCollection
    60.         Try
    61.             Dim fs As New IO.FileStream(FileName, FileMode.Open)
    62.  
    63.             Dim w As New XmlSerializer(GetType(ErrorCollection))
    64.             Dim g As ErrorCollection = CType(w.Deserialize(fs), ErrorCollection)
    65.  
    66.             fs.Close()
    67.  
    68.         Catch ex As Exception
    69.  
    70.             Throw ex
    71.         End Try
    72.     End Function
    73.  
    74. #End Region
    75.  
    76.     Public Class ErrorCode
    77.  
    78.         Private m_strValue As String
    79.         Private m_strDescription As String
    80.  
    81.         <XmlAttributeAttribute("Value")> _
    82.         Public Property Value() As String
    83.             Get
    84.                 Return m_strValue
    85.             End Get
    86.             Set(ByVal Value As String)
    87.                 m_strValue = Value
    88.             End Set
    89.         End Property
    90.         <XmlAttributeAttribute("Description")> _
    91.         Public Property Description() As String
    92.             Get
    93.                 Return m_strDescription
    94.             End Get
    95.             Set(ByVal Value As String)
    96.                 m_strDescription = Value
    97.             End Set
    98.         End Property
    99.  
    100.         Public Sub New(ByVal Value As String, ByVal Description As String)
    101.             m_strValue = Value
    102.             m_strDescription = Description
    103.         End Sub
    104.  
    105.         Public Sub New()
    106.  
    107.         End Sub
    108.  
    109.     End Class
    110.  
    111.     'Public Function FindErrorDescription(ByVal Value As String) As String
    112.     '    For Each e As ErrorCode In Me
    113.     '        If e.Value.ToUpper = Value.ToUpper Then
    114.     '            Return e.Description
    115.     '        End If
    116.     '    Next
    117.     'End Function
    118.  
    119. End Class
    Last edited by ZeroCool; Sep 5th, 2004 at 06:26 PM.
    Visual Baisc 6 (SP5)
    Windows Xp

  2. #2

    Thread Starter
    Hyperactive Member ZeroCool's Avatar
    Join Date
    Feb 2002
    Location
    In front of my computer
    Posts
    423
    Traced it down to:

    VB Code:
    1. 'Create a new XmlSerialzer instance.
    2.         Dim s As New XmlSerializer(GetType(ErrorCollection), root)

    i guess it doesnt like something in the ErrorCollection Class?

    Any Ideas?
    Visual Baisc 6 (SP5)
    Windows Xp

  3. #3

    Thread Starter
    Hyperactive Member ZeroCool's Avatar
    Join Date
    Feb 2002
    Location
    In front of my computer
    Posts
    423
    meh... I must be losing my mind.... i thought that i put everything back the way it was, but i missed that i nested the ErrorCode Class.

    It works fine if the ErrorCode Class isnt Nested.

    Otherwise both the IDE and The App Hang. If i launch if w/o debugging it throws a StackOverFlowException


    meh.... oh well its working now.
    Visual Baisc 6 (SP5)
    Windows Xp

  4. #4
    Hyperactive Member
    Join Date
    Jul 2000
    Location
    Europe, Lithuania
    Posts
    309

    Question Re: Custom Collection Class Serialization [Resolved]

    How to upgrade ErrorCode Class in this example, that I can create nested object.

    Now I can add items like this:

    x.Add(New ErrorCode("123", "321"))


    but can't add subitem like this:

    x(0).Add(New ErrorCode("123", "321"))

    couse ErrorCode Class hasn't Add method.
    I tryied something, but then there are some problems (like ZeroCool described) with serializing.

    Please help.

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