Results 1 to 4 of 4

Thread: Serialization Collections?

  1. #1

    Thread Starter
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577

    Serialization Collections?

    Hi

    Is it possible to serialize collections variables?

    I get the following error on
    oBinaryFormatter.Serialize(oFileStream, _oCollection)


    variables are declare like
    Dim oBinFormatter As BinaryFormatter
    Dim oFileStream As FileStream
    Dim _oCollection As Collection = New Collection()

    An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll

    Additional information: The type Microsoft.VisualBasic.Collection in Assembly Microsoft.VisualBasic, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a is not marked as serializable.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    The collection object is not serializeable so no. You'd have to create a custom collection that is serializable or loop through the collection and serialize each individual object instead.

  3. #3

    Thread Starter
    Fanatic Member Geespot's Avatar
    Join Date
    Oct 2001
    Location
    Birmingham, UK
    Posts
    577
    Thanks but im still having problems....


    I get the following error

    n unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll

    Additional information: The type WindowsApplication1.Form1+mystruct in Assembly WindowsApplication1, Version=1.0.1147.39837, Culture=neutral, PublicKeyToken=null is not marked as serializable.

    VB Code:
    1. Structure mystruct
    2.         Dim Header() As String
    3.         Dim Data() As Byte
    4.     End Structure
    5.  
    6.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    7.         Dim struct(10) As mystruct
    8.         Dim h(5) As String
    9.         Dim d(10) As Byte
    10.  
    11.         '-- Data 1
    12.         h(0) = "Header 1"
    13.         h(2) = "Header 2"
    14.         h(3) = "Header 3"
    15.         d(0) = Asc("W")
    16.         d(1) = Asc("O")
    17.         d(2) = Asc("R")
    18.         d(3) = Asc("K")
    19.         d(4) = Asc("!")
    20.  
    21.         '-- Add Data 1 to struct
    22.         struct(0).Header = h
    23.         struct(0).Data = d
    24.  
    25.  
    26.         '-- Data 2
    27.         h(0) = "Header 4"
    28.         h(2) = "Header 5"
    29.         h(3) = "Header 6"
    30.         d(0) = Asc("N")
    31.         d(1) = Asc("O")
    32.         d(2) = Asc("W")
    33.         d(3) = Asc("!")
    34.  
    35.         '-- Add Data 1 to struct
    36.         struct(1).Header = h
    37.         struct(1).Data = d
    38.  
    39.         Dim oFileStream As IO.FileStream
    40.         Dim oBinaryFormatter As Runtime.Serialization.Formatters.Binary.BinaryFormatter = New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
    41.  
    42.         oFileStream = IO.File.Create("c:\b.txt")
    43.         oBinaryFormatter.Serialize(oFileStream, struct)
    44.         oFileStream.Close()

    My data is structered like above ( but mass amounts of it ), and i just want to easily serialize the lot into a file, but i cant due to that error.

    Am i doing this wrong?

  4. #4
    New Member
    Join Date
    Dec 2002
    Location
    Randolph, NJ
    Posts
    6
    You need to add the serializable attribute to your struct, like this

    VB Code:
    1. <serializable()> Structure mystruct
    2.         Dim Header() As String
    3.         Dim Data() As Byte
    4.     End Structure
    I FOUND YOU

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