473,513 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deserialize to self

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.SoapF ormatter
me = CType(sf.Deserialize(fs), cJob)
End If
End Sub
Nov 20 '05 #1
4 3294
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" <an*******@discussions.microsoft.com> wrote in message
news:10****************************@phx.gbl...
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.SoapF ormatter
me = CType(sf.Deserialize(fs), cJob)
End If
End Sub

Nov 20 '05 #2
* "George Addison" <an*******@discussions.microsoft.com> 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.SoapF ormatter
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>
Nov 20 '05 #3
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.SoapF ormatter
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.SoapF ormatter 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" <an*******@discussions.microsoft.com> wrote in message
news:10****************************@phx.gbl... 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.SoapF ormatter
me = CType(sf.Deserialize(fs), cJob)
End If
End Sub

Nov 20 '05 #4
I appreciate all your feedback! Thanks.
Nov 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
570
by: Carl Mercier | last post by:
Hi! I have 2 different applications/assembly. The first one creates an object and serializes it to a textfile on disk. The second one has the the exact same class (copied/pasted). It reads...
4
1808
by: Bob Rock | last post by:
Hello, I've got an xml stream that I'd need to deserialize into an instance of a given class A. I'd like to create an instance method on class A (method Deserialize) that takes this XML stream...
2
9515
by: Greg | last post by:
I'm writing a class in C# .... I have a collection calls Reports made up of Report objects. I'm trying to deserialize an XML file that looks like : <Reports> <Report> <Title>some title</Title>...
3
5863
by: Amsteel | last post by:
I got something like this in VB.Net <Serializable()> Public Class YQProfileC Inherits CollectionBase Public Sub Save2File(ByVal FileName As String) Dim IFormatter As New BinaryFormatter() Dim...
2
6034
by: PCH | last post by:
I have 2 functions, one to serialize an object, and one to deserialize it. I can serialize just fine, the problem is when I try to deserialize it later... I get an error: {"Invalid...
0
2059
by: Fruber Malcome | last post by:
I'm getting a very weird exception and hoping someone may be able to help. I have an Office Add-In that lives in a .dll (for email reference ai.dll) ai.dll makes calls into the core part of the...
2
11519
by: Thomas S | last post by:
Any suggestions on how to deserialize an object from one line of XML? I'm trying to deserialize multiple objects from one XML document, each object on one line of the file. The serialization is...
4
4629
by: Anbu | last post by:
Hi All, I need to Deserialize the SoapService's SoapEnvelope response object. Here is the Body of the SoapEnvelope received as respone, <q1:serviceResponse...
0
2235
by: connectpalm03-forum | last post by:
I have a class named (MyClassA) in ControlClasses.dll and was able to serialize it to database. Like below SaveTo(MemoryStream stream) { IFormatter formatter = new BinaryFormatter(); ...
0
7162
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7539
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5686
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5090
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3223
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1597
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.