Link to home
Start Free TrialLog in
Avatar of LongDave
LongDave

asked on

Can't De-serialize a data structure

Hi, experts -

I'm a bit new with .Net, but pretty advanced with vb6. What I'm trying to do is take a user defined data structure and serialize/deserialize it. Serialization code is:
            Dim fs As New FileStream(ConnectionInfoFileName, FileMode.Create)
            ' Construct a BinaryFormatter and use it to serialize the data to the stream.
            Dim formatter As New BinaryFormatter
            NewObject = CType(ConnectionInfo, ConnectInfo)

            formatter.Serialize(fs, NewObject)

This seems to work - at least, the file is created and what isn't binary gibberish looks like the various names of the data structure members. (Incidentally, all members of the struct are 'Friend', not 'Public')

De-serialization looks like this:
            Dim fs As New FileStream(ConnectionInfoFileName, FileMode.Open)
            ' Construct a BinaryFormatter and use it to serialize the data to the stream.
            Dim formatter As New BinaryFormatter
            NewObject = CType(formatter.Deserialize(fs), ConnectInfo)

This consistently throws an exception 'Can't find assembly .....'
1) Is it in fact possible to serialize a struct with variable length felds?
2) If so, can you tell me what I'm doing wrong?

Thanks
Avatar of Jayadev Nair
Jayadev Nair
Flag of United States of America image

I believe you do not have the assembly reference where you are trying to deserialize the object. Try adding reference to the dll where the actual structure is defined (if serialization & deserialization occurs in different assembly).
Avatar of LongDave
LongDave

ASKER

When I look at the error message, I believe you are correct, however, I'm not sure how I go about this. The application is a user control that I'm building, and the error occurs when I'm testing in the UserControl Test Container. Where would I find the dll you speak of and how do I create a reference to it? Thanks a lot - I think I'm almost there.
ASKER CERTIFIED SOLUTION
Avatar of Jayadev Nair
Jayadev Nair
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'll I'm trying to do is serialize a data structure. I changed things around a bit since the last response. I've now created a class called CInfo. Here is the entire thing:
******************************************************
Public Class CInfo

    <Serializable()> Public Structure EtherNetInfo
        Public RemoteIP As String       'IP of the remote system
        Public RemotePort As Integer    'remote port
        Public LocalPort As Integer     'remote port
        Public Const EtherNetPassword = "pass45733190"  'password for server card
    End Structure

    <Serializable()> Public Structure ModemInfo
        Public CommPort As Short                'number of the commport to use
        Public PhoneNumber As String            'phone number
        Public Password As String               'password
        Public CallWaitingDisable As Boolean    'see if call waiting is disabled
        Public CallWaitingString As String       'string to disable call waiting
        Public Const EtherNetPassword = "pass45733190"  'password for server card
    End Structure

    <Serializable()> Public Structure DTCCommPort
        Public CommPort As Short        'commport
        Public Password As String        'password
    End Structure

    <Serializable()> Public Structure PortOnly
        Public CommPort As Short        'commport
    End Structure

    <Serializable()> Public Structure ConnectInfo
        Public B3_Eth As EtherNetInfo       'BackBoneBus over Ethernet
        Public B3_Modem As ModemInfo        '   "          "  Modem
        Public L2_Eth As EtherNetInfo       'Link2 Card over Ethernet
        Public L2_CommPort As PortOnly      '   "    "  from a commport
        Public DTC_Modem As ModemInfo       'DTC over a modem
        Public DTC_Direct As DTCCommPort    'DTC over a commport
        Public Location As String           'save the location
        Public Reconnect As Boolean         'see if reconnect at startup
        Public ErrorLogEnableFlag As Boolean    'enable error logging
        Public CommLogEnableFlag As Boolean     'enable comm logging
    End Structure
    Public Sub New()

    End Sub
End Class
*********************************************
Note the structure 'ConnectInfo' is composed of the previous structs above it. In my
main program, I initialize this way:

    Friend ConnectionInfo As New CInfo.ConnectInfo

My project is called 'ConnectControl'. In my \Projects\bin\Debug folder the only dll that is there
is 'ConnectControl.dll'. If I load this as a reference I get:

'CInfo' is ambiguous in the namespace 'ConnectControl'.

I'm starting to feel like a bit of an idiot here :)
Thanks for helping - hope to hear from you (or another expert) soon
I got it working! Thanks for your help! You at least pointed me in the right direction, so I'll give you half points.

Thanks again -

Dave