?
Solved

how to save a vb dictionary to a file

Posted on 2014-11-23
4
Medium Priority
?
123 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
louisfr earned 2000 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 2000 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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

752 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