By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,333 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,333 IT Pros & Developers. It's quick & easy.

Can a constructor deserialize to itself?

P: n/a
I would like an Object's Constructor to create itself by deserializing from XML. Is there an easy way to do this?

I could use a separate function to deserialize to an object but I'd like to do it in the Constructor. Here's an example of what I
mean;

Private Sub Page_Load()
' Sample XML:
' <?xml version=""1.0"" encoding=""UTF-8""?>
' <ObjectType>
' <SomeValue>Hello.</SomeValue>
' </ObjectType>

Dim NewObject As ObjectType = New ObjectType("<?xml version=""1.0""
encoding=""UTF-8""?><ObjectType><SomeValue>Hello.</SomeValue></ObjectType>")

End Sub

Public Class ObjectType
Public SomeValue As System.String

Public Sub New(ByVal xml As System.String)
Dim r As New System.IO.StringReader(xml)
Dim d As New System.XML.Serialization.XmlSerializer(GetType(Obj ectType))
'
' this works but is not very elegant since every property must be set manually.
'
Dim NewMe As New ObjectType
NewMe = d.Deserialize(r)
Me.SomeValue = NewMe.SomeValue

'
' this doesn't work but it's what I'm looking for.
'
Me = d.Deserialize(r)
End Sub

Public Sub New()
End Sub

End Class


Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Stephen,
By the time the constructor runs the object's memory is already created, so
no the constructor cannot deserialize itself.

I would recommend you add a Shared method to the class that does the
deserialization for you..

Something like:
Dim NewObject As ObjectType = ObjectType.FromXml("<?xml version=""1.0"" encoding=""UTF-8""?><ObjectType><SomeValue>Hello.</SomeValue></ObjectType>")

Public Class ObjectType
Public SomeValue As System.String

Public Shared Sub FromXml(ByVal xml As System.String) As ObjectType Dim r As New System.IO.StringReader(xml)
Dim d As New System.XML.Serialization.XmlSerializer(GetType(Obj ectType))

I would then consider adding a "matching" "serialize" method to the class

Public Function ToXml() As String
Dim d as XmlSerializer(...)

Hope this helps
Jay

"Stephen Travis" <st*****@iname.com> wrote in message
news:O2**************@TK2MSFTNGP12.phx.gbl... I would like an Object's Constructor to create itself by deserializing from XML. Is there an easy way to do this?
I could use a separate function to deserialize to an object but I'd like to do it in the Constructor. Here's an example of what I mean;

Private Sub Page_Load()
' Sample XML:
' <?xml version=""1.0"" encoding=""UTF-8""?>
' <ObjectType>
' <SomeValue>Hello.</SomeValue>
' </ObjectType>

Dim NewObject As ObjectType = New ObjectType("<?xml version=""1.0"" encoding=""UTF-8""?><ObjectType><SomeValue>Hello.</SomeValue></ObjectType>")
End Sub

Public Class ObjectType
Public SomeValue As System.String

Public Sub New(ByVal xml As System.String)
Dim r As New System.IO.StringReader(xml)
Dim d As New System.XML.Serialization.XmlSerializer(GetType(Obj ectType)) '
' this works but is not very elegant since every property must be set manually. '
Dim NewMe As New ObjectType
NewMe = d.Deserialize(r)
Me.SomeValue = NewMe.SomeValue

'
' this doesn't work but it's what I'm looking for.
'
Me = d.Deserialize(r)
End Sub

Public Sub New()
End Sub

End Class

Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.