473,379 Members | 1,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Deserialize Only Calls Default Constructor

I have an object that is created like this

Dim MyObj As MyClass

MyObj = New MyClass(objInfo)

The point of this being that the creation of the object requires some
information so that it can set some internal parameters.

When the object is serialised, its state is saved; but not the internal
stuff that was derived from objInfo, because it comes from somewhere else.
So far so good.

When I deserialise the object, it is effectively created like this

MyObj = New MyClass()

so it has to instantiate itself without the objInfo stuff, and therein lies
my problem.

Is there a straight forward way round this, or again is there a pattern that
addresses this problem?

TIA

Charles
Nov 21 '05 #1
5 1879
Hi guys

I was just wondering whether I have become invisible, or do I just ask
awkward questions?

I had hoped that someone would be able to say "oh, you just do this", or
"look at that pattern", but I guess not.

Any thoughts or musings would be welcome.

Thanks.

Charles
"Charles Law" <bl***@nowhere.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
I have an object that is created like this

Dim MyObj As MyClass

MyObj = New MyClass(objInfo)

The point of this being that the creation of the object requires some
information so that it can set some internal parameters.

When the object is serialised, its state is saved; but not the internal
stuff that was derived from objInfo, because it comes from somewhere else.
So far so good.

When I deserialise the object, it is effectively created like this

MyObj = New MyClass()

so it has to instantiate itself without the objInfo stuff, and therein lies my problem.

Is there a straight forward way round this, or again is there a pattern that addresses this problem?

TIA

Charles

Nov 21 '05 #2
Charles Law wrote:
When the object is serialised, its state is saved; but not the internal
stuff that was derived from objInfo, because it comes from somewhere else.
So far so good. I wouldn't say that. You have lost data and you wont be able to recreate
the object.

When I deserialise the object, it is effectively created like this

MyObj = New MyClass() No. It will be created by the constructor
New(info as serializationInfo, context as StreamingContext)

so it has to instantiate itself without the objInfo stuff, and therein
lies my problem. Your problem was in step 1...

Is there a straight forward way round this, or again is there a pattern
that addresses this problem?
Yes, you have to serialise EVERYTHING. (Or something that will allow you to
reconstruct it.)
TIA

Charles


Nov 21 '05 #3
Hi Codo

Thanks for the reply. I have been experimenting a bit since reading your
comments, and have looked a few things up in the MSDN too.

I tried implementing a constructor like the one you mention, to see if it
gets called, but it didn't.

I use the xml serializer and mark my classes <Serializable()>, rather than
implement ISerializable. Does that make a difference?

Charles
"Codo" <Co**@DONOTSPAMME.NET> wrote in message
news:Fv************@newsfe2-gui.ntli.net...
Charles Law wrote:
When the object is serialised, its state is saved; but not the internal
stuff that was derived from objInfo, because it comes from somewhere else. So far so good. I wouldn't say that. You have lost data and you wont be able to recreate
the object.

When I deserialise the object, it is effectively created like this

MyObj = New MyClass()

No. It will be created by the constructor
New(info as serializationInfo, context as StreamingContext)

so it has to instantiate itself without the objInfo stuff, and therein
lies my problem.

Your problem was in step 1...

Is there a straight forward way round this, or again is there a pattern
that addresses this problem?

Yes, you have to serialise EVERYTHING. (Or something that will allow you

to reconstruct it.)
TIA

Charles

Nov 21 '05 #4
Charles Law wrote:
Hi Codo

Thanks for the reply. I have been experimenting a bit since reading your
comments, and have looked a few things up in the MSDN too.

I tried implementing a constructor like the one you mention, to see if it
gets called, but it didn't.

I use the xml serializer and mark my classes <Serializable()>, rather than
implement ISerializable. Does that make a difference?

Charles


I think it does, though I haven't tested them. I have been implementing
ISerializable in vb.net, because I started to have problems with events
(you will LOOOOOOOVE events and <Serializable()>. You will want to move to
C#). I think the constructor New(info as SerializationInfo, context as
StreamingContext) only gets called when you implement ISerializable.
Otherwise dotnet will execute some funny internal (you may want ask Bill
Gates) reconstruction of your objects. Try reading ISerialisable on the
help veeeery slowly. It has lots of things that can confuse you very
easily (I was)... Hope this helps!
Nov 21 '05 #5
Hi Codo

I will proceed with caution ... thanks.

Charles
"Codo" <Co**@DONOTSPAMME.NET> wrote in message
news:yA**************@newsfe5-gui.ntli.net...
Charles Law wrote:
Hi Codo

Thanks for the reply. I have been experimenting a bit since reading your
comments, and have looked a few things up in the MSDN too.

I tried implementing a constructor like the one you mention, to see if it gets called, but it didn't.

I use the xml serializer and mark my classes <Serializable()>, rather than implement ISerializable. Does that make a difference?

Charles

I think it does, though I haven't tested them. I have been implementing
ISerializable in vb.net, because I started to have problems with events
(you will LOOOOOOOVE events and <Serializable()>. You will want to move

to C#). I think the constructor New(info as SerializationInfo, context as
StreamingContext) only gets called when you implement ISerializable.
Otherwise dotnet will execute some funny internal (you may want ask Bill
Gates) reconstruction of your objects. Try reading ISerialisable on the
help veeeery slowly. It has lots of things that can confuse you very
easily (I was)... Hope this helps!

Nov 21 '05 #6

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

Similar topics

0
by: Lefevre | last post by:
Hello I recently had troubles with a class inheritance hierarchy. I solved it, but it didn't satisfied me. I found the solution using this forum :) Actualy i found the following message...
2
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>...
4
by: Sebastien Tardif | last post by:
Subject: XmlSerializer.Deserialize complain when root declare the namespace If I do XmlSerializer.Deserialize( myString ) and myString is: String myString = "<?xml version=\"1.0\"...
0
by: JMD | last post by:
I have a set of schemas for two different XML files that share common structures through the use of "import". Eventually, the main XSD file (which just gathers pieces from the common stuff to...
0
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...
1
by: Stephen Travis | last post by:
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...
1
by: John | last post by:
Hi, Maybe someone can help me with the following: "The first task by any derived class constructor is to call it’s direct or indirect base class constructor implicitly or explicitly", reads the...
6
by: Val | last post by:
How can I serialize/deserialize an object into a string. Existing examples seem to be showing this operation for files only. Thansk
1
by: Hoss | last post by:
Quick Remark. I have a class that looks like this public Class { public int Attribu
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.