none
Trying to serialize a list of structures RRS feed

  • Question

  • I am new to Visual Studio and trying to transfer VBA code from Excel into a stand alone Visual Basic application.  Everything seems to work well, except I always get a "SerializationException" when I try to write datastructures to a file.  Searching the internet always points me to old (2005ish) information.

    This code always fails, displaying the list count show the proper number of items.

     Using fs As New FileStream(outputFileName, FileMode.OpenOrCreate)
               Dim bf As New BinaryFormatter
                    Try
                        bf.Serialize(fs, dataFile)      ' dataFile is a list of multiple items with the same structure
                    Catch ex As Exception
                        MessageBox.Show(CStr(dataFile.Count), "Fail to Serialize")
                    End Try
    End Using

    A recent article from Microsoft cited a bug if static variables are used (which they aren't) and suggested using xml serialization rather than binary.  I am not rel familiar with xml, but it seems to be text based and I would like this data compressed in binary format as it is mostly numeric.

    Any suggestions how to eliminate this exception?  Thanks

    Friday, February 22, 2019 4:15 PM

Answers

  • I've never had to do this with a structure but have with classes so the following should work.

    <Serializable()>
    Public Structure Employee
        Public GivenName As String
        Public FamilyName As String
        Public PhoneExtension As Long
        Private _salary As Decimal
    End Structure
    Code flow example using classes (extremely simple where there is a list and the OP wanted one item but it can store multiple instances and multiple types) from here.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by jhnmccllm Friday, February 22, 2019 6:27 PM
    Friday, February 22, 2019 4:43 PM
    Moderator

All replies

  • I've never had to do this with a structure but have with classes so the following should work.

    <Serializable()>
    Public Structure Employee
        Public GivenName As String
        Public FamilyName As String
        Public PhoneExtension As Long
        Private _salary As Decimal
    End Structure
    Code flow example using classes (extremely simple where there is a list and the OP wanted one item but it can store multiple instances and multiple types) from here.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by jhnmccllm Friday, February 22, 2019 6:27 PM
    Friday, February 22, 2019 4:43 PM
    Moderator
  • This is exactly what I do:

    <Serializable()> Public Structure datasetitem    'this creates data lines for saving files
        Dim Time As Decimal    'this is actually a date, but VB will not convert Decimals to Dates
        Dim Point As Location
        Dim Yield As Decimal
        Dim Protein As Decimal
    End Structure

    Then have:

    Dim dataFile As New List (Of datasetitem)

    When I replace the Serialization code with displaying the data, the dataFile appears correctly filled.   I have tried both Array and List for the dataFile, but they cannot be serialized.  Perhaps the Location structure also needs the Serializable Attribute?  Or should I just encapsulate all data into a Class?

    Friday, February 22, 2019 5:42 PM
  • That seems to be it.  Not sure why your response triggered the obvious, but it did.  Thanks.  I just added the Serializable attribute to the Location structure as well and it runs fine now.  That was more of a challenge than it should have been.
    Friday, February 22, 2019 5:45 PM
  • Glad you are okay now :-)

    If this thread is resolved please close it out by marking any replies which were helpful or provided the solution so that when others with the same problem will not the solution.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, February 22, 2019 6:24 PM
    Moderator