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

Deserializing into a pre-exisiting object

P: n/a
What happens to an object which is not nothing has another object
deserialized into it? I assume that the data in it is completely destroyed by
the new object.

I am interested because I have some strange persisting data elements
cropping up whose origins I am attempting to identify. I was wondering if
deserializing in the manner described above might be responsible if the
original object is not actually destroyed.
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Andrew,

I do understand almost nothing from your message. However an object that has
a reference to or from another existing object will never be released.

I hope this helps,

Cor
Nov 21 '05 #2

P: n/a
"Andrew E Chapman" <An************@discussions.microsoft.com> wrote in
message news:9D**********************************@microsof t.com...
What happens to an object which is not nothing has another object
deserialized into it?


How would you achieve this, anyway? I've always deserialised my
objects using a Constructor for the class, so the object /can't/ exist
already.

Public Sub New( _
ByVal info As System.Runtime.Serialization.SerializationInfo _
, ByVal context As System.Runtime.Serialization.StreamingContext _
)

Regards,
Phill W.
Nov 21 '05 #3

P: n/a
Apologies for my opacity. The code below illustrates what I mean. AThing is
declared and the constructor is not called. The object is created with the
first call to the Deserialize sub, but what happens to it with the second? I
had thought that it would be recreated and that the first deserialized AThing
would be replaced by the second. Is this what occurs?

Dim AThing As Thing
....

'1
DeserializeThing(FileNameA, AThing)

'Do some stuff to AThing

'2
DeserializeThing(FileNameB, AThing)

'Is AThing completely reconstituted by call 2?
....
sub DeserializeThing(byval fileName as string, byval T as Thing)
Dim fs As FileStream = New FileStream(fileName, FileMode.Open)
Dim bf As New BinaryFormatter
bf.AssemblyFormat = Formatters.FormatterAssemblyStyle.Simple
T = DirectCast(bf.Deserialize(fs), Thing)
fs.Close()
end sub
Nov 21 '05 #4

P: n/a
"Andrew E Chapman" <An************@discussions.microsoft.com> wrote in
message news:DE**********************************@microsof t.com...
sub DeserializeThing(byval fileName as string, byval T as Thing)
T = DirectCast(bf.Deserialize(fs), Thing)
end sub DeserializeThing(FileNameB, AThing)
' Is AThing completely reconstituted ... ?


It certainly should /not/ be, because AThing is passed ByVal to the
method and yes; I realise it's an reference type.

The deserialisation creates a new instance of a Thing object, and
this is assigned to the /local/ variable T, i.e. a "pointer" to the new
object is stored in T. Because you're only passing the "pointer" to
AThing by value, you're method /can't/ assign a value to it, i.e. you
can't "throw away" the current instance in AThing and replace it with
a new one, which is what you're trying to do, and why your object
"seems" to be retaining it's previous values - it is!!

Pass your Thing (ooh, err, missus) into the method ByRef, and you'll
be able to "return" the new, deserialised instance of the object "into"
AThing.
Better still, make this method into a function and return the new
object explicitly, as in

Function DeserializeThing(byval fileName as string) as Thing
T = DirectCast(bf.Deserialize(fs), Thing)
Return T
End Function
.. . .
AThing = DeserializeThing( "file" )

HTH,
Phill W.
Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.