Solved

how to save a vb dictionary to a file

Posted on 2014-11-23
4
116 Views
Last Modified: 2014-11-25
I have this dictionary
Public misgroupbox As New Dictionary(Of String, MisGb) 

Open in new window


My dictionary takes its type from the following class:
<Serializable()> Public Class MisGb
    Inherits GroupBox
#Region " properties ..."
    Private _id As String
    Public Property id() As String
        Get
            Return _id
        End Get
        Set(ByVal value As String)
            _id = value
        End Set
    End Property

    Private _tipo As String
    Public Property tipo() As String
        Get
            Return _tipo
        End Get
        Set(ByVal value As String)
            _tipo = value
        End Set
    End Property

    Public _posX As Integer
    Public Property posX() As Integer
        Get
            Return _posX
        End Get
        Set(ByVal value As Integer)
            _posX = value
        End Set
    End Property

    Public _posY As Integer
    Public Property posY() As Integer
        Get
            Return _posY
        End Get
        Set(ByVal value As Integer)
            _posY = value
        End Set
    End Property
#End Region

    Public Sub New()
        ')
        '
        MyBase.New()
    End Sub
End Class

Open in new window


Im trying to save my dictionary to a file using this code:
        Dim fs As IO.FileStream = New IO.FileStream("c:\test.bin", IO.FileMode.OpenOrCreate)
        Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
        bf.Serialize(fs, misgroupbox)

Open in new window


But I always receive the following message: example
Did you know what Im doing wrong?
0
Comment
Question by:altariamx2003
  • 2
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points
ID: 40461533
The default behavior to serialize an object is to serialize all public and private fields, including those inherited from base class. It would be a security problem if you could make a class serializable by deriving from it. So, the default behavior won't work if the base class is not serializable.

Implement the ISerializable interface to control the serialization process. See http://msdn.microsoft.com/en-us/library/System.Runtime.Serialization.ISerializable.aspx
0
 

Author Comment

by:altariamx2003
ID: 40462394
I made this changes:
<Serializable()> _
Public Class MisGb
    Inherits GroupBox
    Implements ISerializable

#Region "Initializers"
    Public Sub New()
    End Sub
#End Region

#Region "Properties"
    Private _id As String = Nothing
    <Browsable(True)> _
    Public Property id() As String
        Get
            Return _id
        End Get
        Set(ByVal value As String)
            _id = value
        End Set
    End Property

    Private _tipo As String = Nothing
    <Browsable(True)> _
    Public Property tipo() As String
        Get
            Return _tipo
        End Get
        Set(ByVal value As String)
            _tipo = value
        End Set
    End Property

    Public _posX As Integer = Nothing
    <Browsable(True)> _
    Public Property posX() As Integer
        Get
            Return _posX
        End Get
        Set(ByVal value As Integer)
            _posX = value
        End Set
    End Property

    Public _posY As Integer = Nothing
    <Browsable(True)> _
    Public Property posY() As Integer
        Get
            Return _posY
        End Get
        Set(ByVal value As Integer)
            _posY = value
        End Set
    End Property
#End Region



#Region "Serialization"
    Public Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        _id = info.GetString("id")
        _tipo = info.GetString("tipo")
        _posX = info.GetString("posX")
        _posY = info.GetString("posY")
    End Sub

    Public Sub GetObjectData(ByVal info As System.Runtime.Serialization.SerializationInfo, ByVal context As System.Runtime.Serialization.StreamingContext) Implements System.Runtime.Serialization.ISerializable.GetObjectData
        info.AddValue("id", _id)
        info.AddValue("tipo", _tipo)
        info.AddValue("posX", _posX)
        info.AddValue("posY", _posY)
    End Sub
#End Region
End Class

Open in new window


And now i can save the dictionary to a file without problems,  thanks a lot experts.

But now I see another problem, If I previous add controls like labels, button, etc... to the groupbox (my custom class), when I save the dictionary to a file thouse controls inside are not saved to.

It is possible to save it with the controls inside????
0
 
LVL 11

Assisted Solution

by:louisfr
louisfr earned 500 total points
ID: 40463932
The GroupBox doesn't know how to save them. You'll need to do it in your GetObjectData method.
0
 

Author Comment

by:altariamx2003
ID: 40464629
thanks
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Simple Injector with Web Service 4 43
HTML - Color not displaying correctly in EMAIL. 6 41
C# Gridview 1 33
SQL Server Connection String through a VPN 8 29
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question