Solved

how to save a vb dictionary to a file

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

828 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