Solved

how to save a vb dictionary to a file

Posted on 2014-11-23
4
121 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 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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 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