10 Replies - 3719 Views - Last Post: 18 November 2008 - 10:52 PM Rate Topic: -----

#1 Ameel   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 19-June 08

Serialization. Not very good at it.. Please see and help

Posted 18 November 2008 - 03:55 PM

Hi. I am using Synfusion Essential Studio (from work, heyhey :D )

Been fooling with it, creating a custom object. Code is provided below. However, I'm not sure how to serialize the whole thing. When I try to save, I realise my properties have NOT been saved... :( Please help.
Thanks

<Serializable()> _
 Public Class Narrationtool : Inherits Group


	Private m_outerRect As Syncfusion.Windows.Forms.Diagram.Rectangle = Nothing
	Private m_textNode As TextNode = Nothing
	Private m_strDescription As String = "Custom Symbol"

	Public ztype As String = "Narration"
	Private _LocW As String
	Private _LocX As String
	Private _LocY As String
	Private _LocZ As String
	Private _Data As String
	Private _Music As String
	Private _Background As String
	Private _Character As String
	Private _PermaFlag As String
	Private _PermaStory As String
	Private _Go2 As String
	Private _ComeFrom As String
	Private _connectionstatus As String
	Private _XtraVar1 As String
	Private _XtraVar2 As String
	Private _XtraVar3 As String
	Public Property connectionstatus() As String
		Get
			Return _connectionstatus
		End Get
		Set(ByVal value As String)
			_connectionstatus = value
		End Set
	End Property

	Public Property ComeFrom() As String
		Get
			Return _ComeFrom
		End Get
		Set(ByVal value As String)
			_ComeFrom = value
		End Set
	End Property

	Public Property locW() As String
		Get
			Return _locW
		End Get
		Set(ByVal value As String)
			_locW = value
		End Set
	End Property

	Public Property LocX() As String
		Get
			Return _LocX
		End Get
		Set(ByVal value As String)
			_LocX = value
		End Set
	End Property

	Public Property LocY() As String
		Get
			Return _LocY
		End Get
		Set(ByVal value As String)
			_LocY = value
		End Set
	End Property

	Public Property locZ() As String
		Get
			Return _locZ
		End Get
		Set(ByVal value As String)
			_locZ = value
		End Set
	End Property



	Public Property data() As String
		Get
			Return _data
		End Get
		Set(ByVal value As String)
			_data = value
		End Set
	End Property

	Public Property music() As String
		Get
			Return _music
		End Get
		Set(ByVal value As String)
			_music = value
		End Set
	End Property

	Public Property background() As String
		Get
			Return _background
		End Get
		Set(ByVal value As String)
			_background = value
		End Set
	End Property

	Public Property character() As String
		Get
			Return _character
		End Get
		Set(ByVal value As String)
			_character = value
		End Set
	End Property

	Public Property permaflag() As String
		Get
			Return _permaflag
		End Get
		Set(ByVal value As String)
			_permaflag = value
		End Set
	End Property

	Public Property permastory() As String
		Get
			Return _permastory
		End Get
		Set(ByVal value As String)
			_permastory = value
		End Set
	End Property

	Public Property go2() As String
		Get
			Return _go2
		End Get
		Set(ByVal value As String)
			_go2 = value
		End Set
	End Property

	Public Property xtravar1() As String
		Get
			Return _xtravar1
		End Get
		Set(ByVal value As String)
			_xtravar1 = value
		End Set
	End Property

	Public Property xtravar2() As String
		Get
			Return _xtravar2
		End Get
		Set(ByVal value As String)
			_xtravar2 = value
		End Set
	End Property

	Public Property xtravar3() As String
		Get
			Return _xtravar3
		End Get
		Set(ByVal value As String)
			_xtravar3 = value
		End Set
	End Property


	Public Sub New()
		Me.m_outerRect = New Syncfusion.Windows.Forms.Diagram.Rectangle(0, 0, 86, 20)
		Me.m_outerRect.Name = "Rectangle"
		Me.m_outerRect.FillStyle.Type = Syncfusion.Windows.Forms.Diagram.FillStyleType.LinearGradient
		Me.m_outerRect.FillStyle.ForeColor = System.Drawing.Color.Orange
		Me.m_outerRect.FillStyle.Color = Color.Orange
		Me.m_outerRect.ShadowStyle.Visible = True
		Me.m_outerRect.EditStyle.AllowSelect = False
		Me.AppendChild(m_outerRect)

		Me.m_textNode = New TextNode("Narration")
		Me.m_textNode.FontStyle.Size = 15
		Dim size As New Size(86, 20)
		Dim sizeF As SizeF
		sizeF = size.op_Implicit(size)
		Me.m_textNode.Size = sizeF
		Me.m_textNode.PinPoint = New PointF(43, 10)
		Me.m_textNode.EditStyle.AllowSelect = False
		Me.AppendChild(m_textNode)
		Me.zType = "Narration"
	End Sub

	Public Sub New(ByVal src As Narrationtool)
		MyBase.New(src)
		' update reference
		m_outerRect = CType(Me.GetChild(0), Syncfusion.Windows.Forms.Diagram.Rectangle)
		m_textNode = CType(Me.GetChild(1), TextNode)
	End Sub

	''' <summary>
	''' Serialization constructor for symbols.
	''' </summary>
	''' <param name="info">Serialization state information</param>
	''' <param name="context">Streaming context information</param>
	Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
		MyBase.New(info, context)
		Me.m_strDescription = info.GetString("strDescription")
	End Sub
	''' <summary>
	''' This property provides an example of adding a member variable to a custom symbol
	''' and demonstrates how to handle serialization for this property.
	''' </summary>
	<Browsable(True), Category("General"), Description("Description of the symbol"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
	Public Property Description() As String
		Get
			Return Me.m_strDescription
		End Get
		Set(ByVal value As String)
			Me.m_strDescription = value
		End Set
	End Property
	''' <summary>
	''' Populates a SerializationInfo with the data needed to
	''' serialize the target object.
	''' </summary>
	''' <param name="info">SerializationInfo object to populate</param>
	''' <param name="context">Destination streaming context</param>
	Protected Overrides Sub GetObjectData(ByVal info As SerializationInfo, ByVal context As StreamingContext)
		MyBase.GetObjectData(info, context)

		' Additional member variables are serialized here
		info.AddValue("strDescription", Me.m_strDescription)
	End Sub

	Public Overrides Function Clone() As Object
		Return New Narrationtool(Me)
	End Function

End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Serialization. Not very good at it.. Please see and help

#2 magicmonkey   User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Serialization. Not very good at it.. Please see and help

Posted 18 November 2008 - 08:49 PM

Is nothing getting serialized or is only Description getting serialized? You have overriden the serialization and have only added the one property. Maybe post your serialization code as well so we can see what formatter you are using.
Was This Post Helpful? 0
  • +
  • -

#3 Ameel   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 19-June 08

Re: Serialization. Not very good at it.. Please see and help

Posted 18 November 2008 - 09:19 PM

View Postmagicmonkey, on 18 Nov, 2008 - 07:49 PM, said:

Is nothing getting serialized or is only Description getting serialized? You have overriden the serialization and have only added the one property. Maybe post your serialization code as well so we can see what formatter you are using.


Actually I have no idea what your talking about.
Description is getting serialized. The properties (Which I added) aren't.
Also, I do not know how to serialize AT ALL.
Syncfusion's components have their own way for saving serialied stuff.
Basically the code for saving is just:
Diagram1.SaveBinary(Me.fileName)




I do not mind NOT using their method, if I learn how to do normal/proper serialization. Any help on that?
Was This Post Helpful? 0
  • +
  • -

#4 magicmonkey   User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Serialization. Not very good at it.. Please see and help

Posted 18 November 2008 - 10:01 PM

Okay let me try to help you out,

  Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        MyBase.New(info, context)
        Me.m_strDescription = info.GetString("strDescription")
   End Sub

    Protected Overrides Sub GetObjectData(ByVal info As SerializationInfo, ByVal context As StreamingContext)
        MyBase.GetObjectData(info, context)

        ' Additional member variables are serialized here
        info.AddValue("strDescription", Me.m_strDescription)
    End Sub



The two subs above are the problem, you usually only use them when you want to implement your own serialization logic. If you want to keep them then you have to manually add a line of code to add and retrieve each property. The most common reason for doing it this way is to manage versioning as new fields are added down the road.

Now if you just want to let .NET do all the serialization for you then you have to get rid of the above 2 subs, simply comment them out and rerun the code and see what happens!
Was This Post Helpful? 0
  • +
  • -

#5 magicmonkey   User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Serialization. Not very good at it.. Please see and help

Posted 18 November 2008 - 10:13 PM

I missed the second part of you question about how to serialize a class all on your own. It is pretty simple. First thing is to mark the class with the <Serializable()> attribute as your class already has. The next step is to determine what formatter to use. The most common are XML and BINARY (SOAP is now obsolete). XML is easy to read & edit from outside your application. Binary is faster and smaller and stores private members as well, XML only stores public members.

Here is an example of serializing a Generic List(Of String) collection using the BinaryFormatter...

         Dim x As New List(Of String)
        x.Add("Hi")
        x.Add("Bye")

        Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter

        Using fs As New IO.FileStream(Application.StartupPath & "\TestBinaryFormatter.dat", IO.FileMode.Create)
            bf.Serialize(fs, x)
        End Using

        Dim y As List(Of String)
        Using fs As New IO.FileStream(Application.StartupPath & "\TestBinaryFormatter.dat", IO.FileMode.Open)
            y = DirectCast(bf.Deserialize(fs), List(Of String))
        End Using



Here is an example using an XML Formatter...

        Dim x As New List(Of String)
        x.Add("Hi")
        x.Add("Bye")
        
        Dim xmls As New Xml.Serialization.XmlSerializer(GetType(List(Of String)))
        Using xw As New Xml.XmlTextWriter(Application.StartupPath & "\TestXMLSerialization.xml", System.Text.Encoding.Unicode)
            xmls.Serialize(xw, x)
        End Using

        Dim y As List(Of String)
        Using xr As New Xml.XmlTextReader(Application.StartupPath & "\TestXMLSerialization.xml")
            y = DirectCast(xmls.Deserialize(xr), List(Of String))
        End Using


Was This Post Helpful? 0
  • +
  • -

#6 Ameel   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 19-June 08

Re: Serialization. Not very good at it.. Please see and help

Posted 18 November 2008 - 10:15 PM

I thought that sounded too good to be true :(

It saves without any problem. However, Its poping up message when I try to load:
The constructor to deserialize an object of type "Narrationtool" was not found.



Just wondering. How do I to "manually add a line of code to add and retrieve each property."? Could you give me an example. I do not mind working with that.

Thanks mate,
really appreciate your help, as always

This post has been edited by Ameel: 18 November 2008 - 10:16 PM

Was This Post Helpful? 0
  • +
  • -

#7 magicmonkey   User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Serialization. Not very good at it.. Please see and help

Posted 18 November 2008 - 10:20 PM

Ah, the base class must implement Implements System.Runtime.Serialization.ISerializable, therefore it is looking for the constructor, the New Sub, to deserialize the data.

Let me think about this for a second...
Was This Post Helpful? 0
  • +
  • -

#8 Ameel   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 19-June 08

Re: Serialization. Not very good at it.. Please see and help

Posted 18 November 2008 - 10:21 PM

View Postmagicmonkey, on 18 Nov, 2008 - 09:13 PM, said:

I missed the second part of you question about how to serialize a class all on your own. It is pretty simple. First thing is to mark the class with the <Serializable()> attribute as your class already has. The next step is to determine what formatter to use. The most common are XML and BINARY (SOAP is now obsolete). XML is easy to read & edit from outside your application. Binary is faster and smaller and stores private members as well, XML only stores public members.

Here is an example of serializing a Generic List(Of String) collection using the BinaryFormatter...

         Dim x As New List(Of String)
        x.Add("Hi")
        x.Add("Bye")

        Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter

        Using fs As New IO.FileStream(Application.StartupPath & "\TestBinaryFormatter.dat", IO.FileMode.Create)
            bf.Serialize(fs, x)
        End Using

        Dim y As List(Of String)
        Using fs As New IO.FileStream(Application.StartupPath & "\TestBinaryFormatter.dat", IO.FileMode.Open)
            y = DirectCast(bf.Deserialize(fs), List(Of String))
        End Using



Here is an example using an XML Formatter...

        Dim x As New List(Of String)
        x.Add("Hi")
        x.Add("Bye")
        
        Dim xmls As New Xml.Serialization.XmlSerializer(GetType(List(Of String)))
        Using xw As New Xml.XmlTextWriter(Application.StartupPath & "\TestXMLSerialization.xml", System.Text.Encoding.Unicode)
            xmls.Serialize(xw, x)
        End Using

        Dim y As List(Of String)
        Using xr As New Xml.XmlTextReader(Application.StartupPath & "\TestXMLSerialization.xml")
            y = DirectCast(xmls.Deserialize(xr), List(Of String))
        End Using



That sounds good. I'll give it a shot.
Thanks for teaching me serialization ^__^
Was This Post Helpful? 0
  • +
  • -

#9 magicmonkey   User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Serialization. Not very good at it.. Please see and help

Posted 18 November 2008 - 10:34 PM

Okay you must implement the ISerializable constructor and GetObjectData in the derived class when the base class implements ISerializable. I thought there might be a way around it...


If you want to extend your original code then you need to add a line for each property similar to the description line...

Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)  
      MyBase.New(info, context)  
      Me.m_strDescription = info.GetString("strDescription")  
      Me._connectionstatus = info.GetString("connectionstatus") 'Do this for each property, the name in quotes must match in the GetObjectData, also use the correct datatype when using Get, even thou all your properties are strings...
 End Sub  
  
  Protected Overrides Sub GetObjectData(ByVal info As SerializationInfo, ByVal context As StreamingContext)  
      MyBase.GetObjectData(info, context)  
  
      ' Additional member variables are serialized here  
      info.AddValue("strDescription", Me.m_strDescription)  
      info.AddValue("connectionstatus", Me._connectionstatus) 'Add the properties here, make sure names match between two subs

  End Sub  


Was This Post Helpful? 1
  • +
  • -

#10 Ameel   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 19-June 08

Re: Serialization. Not very good at it.. Please see and help

Posted 18 November 2008 - 10:44 PM

Hi. I tried to serialize as follows:

		Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
			Dim bf As New System.Runtime.Serialization.Formatters.Soap.SoapFormatter
			Using fs As New IO.FileStream(Application.StartupPath & "\TestBinaryFormatter.dat", IO.FileMode.Create)
				bf.Serialize(fs, Model1.Nodes)
			End Using
		End Sub


		Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
			Dim bf As New System.Runtime.Serialization.Formatters.Soap.SoapFormatter
			Using fs As New IO.FileStream(Application.StartupPath & "\TestBinaryFormatter.dat", IO.FileMode.Open)
				MsgBox("1")
				Dim y As NodeCollection = DirectCast(bf.Deserialize(fs), NodeCollection)
				Dim i As Integer = y.Count
				For i = 0 To y.Count - 1
					Model1.AppendChild(y(i))
				Next

			End Using

		End Sub



It's no good.
It saves.
But when I load, it shows the stuff again, still with "description", but not the properties that i needed :/

What I did to the Class that needed serialization was to get rid of the 2 subs you mentioned. Should I have gotten rid of other stuff as well?

View Postmagicmonkey, on 18 Nov, 2008 - 09:34 PM, said:

Okay you must implement the ISerializable constructor and GetObjectData in the derived class when the base class implements ISerializable. I thought there might be a way around it...


If you want to extend your original code then you need to add a line for each property similar to the description line...

Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)  
      MyBase.New(info, context)  
      Me.m_strDescription = info.GetString("strDescription")  
      Me._connectionstatus = info.GetString("connectionstatus") 'Do this for each property, the name in quotes must match in the GetObjectData, also use the correct datatype when using Get, even thou all your properties are strings...
 End Sub  
  
  Protected Overrides Sub GetObjectData(ByVal info As SerializationInfo, ByVal context As StreamingContext)  
      MyBase.GetObjectData(info, context)  
  
      ' Additional member variables are serialized here  
      info.AddValue("strDescription", Me.m_strDescription)  
      info.AddValue("connectionstatus", Me._connectionstatus) 'Add the properties here, make sure names match between two subs

  End Sub  




Zzz. I'm getting confused with normal serialization. Not sure what exactly im serializing anymore ~_~

will take a rest then try the above code.

Thanks again for all your trouble magicmonkey
wuv u hard
Was This Post Helpful? 0
  • +
  • -

#11 Ameel   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 19-June 08

Re: Serialization. Not very good at it.. Please see and help

Posted 18 November 2008 - 10:52 PM

View Postmagicmonkey, on 18 Nov, 2008 - 09:34 PM, said:

Okay you must implement the ISerializable constructor and GetObjectData in the derived class when the base class implements ISerializable. I thought there might be a way around it...


If you want to extend your original code then you need to add a line for each property similar to the description line...

Protected Sub New(ByVal info As SerializationInfo, ByVal context As StreamingContext)  
      MyBase.New(info, context)  
      Me.m_strDescription = info.GetString("strDescription")  
      Me._connectionstatus = info.GetString("connectionstatus") 'Do this for each property, the name in quotes must match in the GetObjectData, also use the correct datatype when using Get, even thou all your properties are strings...
 End Sub  
  
  Protected Overrides Sub GetObjectData(ByVal info As SerializationInfo, ByVal context As StreamingContext)  
      MyBase.GetObjectData(info, context)  
  
      ' Additional member variables are serialized here  
      info.AddValue("strDescription", Me.m_strDescription)  
      info.AddValue("connectionstatus", Me._connectionstatus) 'Add the properties here, make sure names match between two subs

  End Sub  




Exactly what I needed.

wuv u even more now.

Thanks again magicmonkey
Ur da best ^__^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1