PC Review


Reply
Thread Tools Rate Thread

Deserialize to self

 
 
George Addison
Guest
Posts: n/a
 
      9th Dec 2003
I understand this might not be the optimal method of
deserialization, but how can I deserialize a class to
itself? Something like:

Public Sub New(Optional ByVal filename as string =
Nothing)
If Not IsNothing(filename) then
fs = New System.IO.FileStream(filename,
System.IO.FileMode.Open)
Dim sf As New
System.Runtime.Serialization.Formatters.Soap.SoapFormatter
me = CType(sf.Deserialize(fs), cJob)
End If
End Sub
 
Reply With Quote
 
 
 
 
Codemonkey
Guest
Posts: n/a
 
      9th Dec 2003
How about creating a shared method called "Load" or something that returns
the deserialized instance?

Alternatively, but messy, you could deserialize to a local variable instance
and then copy all its fields to the current instance.

Hope this helps,

Trev.

"George Addison" <(E-Mail Removed)> wrote in message
news:105701c3be7b$cf8d3aa0$(E-Mail Removed)...
> I understand this might not be the optimal method of
> deserialization, but how can I deserialize a class to
> itself? Something like:
>
> Public Sub New(Optional ByVal filename as string =
> Nothing)
> If Not IsNothing(filename) then
> fs = New System.IO.FileStream(filename,
> System.IO.FileMode.Open)
> Dim sf As New
> System.Runtime.Serialization.Formatters.Soap.SoapFormatter
> me = CType(sf.Deserialize(fs), cJob)
> End If
> End Sub



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      9th Dec 2003
* "George Addison" <(E-Mail Removed)> scripsit:
> I understand this might not be the optimal method of
> deserialization, but how can I deserialize a class to
> itself? Something like:
>
> Public Sub New(Optional ByVal filename as string =
> Nothing)
> If Not IsNothing(filename) then
> fs = New System.IO.FileStream(filename,
> System.IO.FileMode.Open)
> Dim sf As New
> System.Runtime.Serialization.Formatters.Soap.SoapFormatter
> me = CType(sf.Deserialize(fs), cJob)
> End If
> End Sub


You cannot assign anything to 'Me'. I would create a shared method in
the class which returns an instance of the class.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
 
Reply With Quote
 
Jay B. Harlow [MVP - Outlook]
Guest
Posts: n/a
 
      9th Dec 2003
George,
Short answer: You Don't!!!!

Long answer: You cannot deserialize a class to "me" as by the time the
constructor executes the object has already been created. Deserialization
creates a new object!

A couple of alternatives would be to add a Factory Method to your class that
deserializes an instance of the class and return it.

Public Class cJob

Private m_attr1 As String
Private m_attr2 As String

> Public Shared Function FromFile(ByVal filename as string) As cJob
> fs = New System.IO.FileStream(filename,
> System.IO.FileMode.Open)
> Dim sf As New
> System.Runtime.Serialization.Formatters.Soap.SoapFormatter
> return DirectCast(sf.Deserialize(fs), cJob)
> End


End Class

Note that the above Factory Method is a Shared method (you do not need an
instance of the class to use it) and it returns an instance of the class.

A more involved alternative is to have a class private to cJob that contains
all of cJOb's data, then each method of cJob would delegate to this private
class

Public Class Job

Private Class JobData
Private m_attr1 As String
Private m_attr2 As String
End Class

Private Readonly m_data As JobData

Public Sub New()
m_data = new JobData
End Sub

Public Sub New(ByVal filename as string)
> fs = New System.IO.FileStream(filename,
> System.IO.FileMode.Open)
> Dim sf As New
> System.Runtime.Serialization.Formatters.Soap.SoapFormatter

m_data = DirectCast(sf.Deserialize(fs), JobData)
End

End Class

Of course this runs into problems when you attempt to serialize, as you
cannot really serialize the Job object, you need to serialize the JobData
object. I would favor the Factory Method.

Hope this helps
Jay

"George Addison" <(E-Mail Removed)> wrote in message
news:105701c3be7b$cf8d3aa0$(E-Mail Removed)...
> I understand this might not be the optimal method of
> deserialization, but how can I deserialize a class to
> itself? Something like:
>
> Public Sub New(Optional ByVal filename as string =
> Nothing)
> If Not IsNothing(filename) then
> fs = New System.IO.FileStream(filename,
> System.IO.FileMode.Open)
> Dim sf As New
> System.Runtime.Serialization.Formatters.Soap.SoapFormatter
> me = CType(sf.Deserialize(fs), cJob)
> End If
> End Sub



 
Reply With Quote
 
George Addison
Guest
Posts: n/a
 
      9th Dec 2003
I appreciate all your feedback! Thanks.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Deserialize an XML Jani Järvinen [MVP] Microsoft ASP .NET 0 3rd Nov 2009 03:23 PM
Re: Deserialize an XML Family Tree Mike Microsoft C# .NET 0 2nd Nov 2009 10:02 PM
Deserialize to 'this' Hoss Microsoft C# .NET 1 23rd Jan 2007 03:23 PM
Deserialize XML where XML may contain XML nick_nw Microsoft C# .NET 1 5th Jul 2006 11:32 AM
XML deserialize Stream works ok but deserialize from XmlNodeReader fails Samuel R. Neff Microsoft VB .NET 4 8th Feb 2005 03:37 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:39 PM.