Solved

how to save a vb dictionary to a file

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now