473,725 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Serialize 'null'?

Hi,

I've created a UserControl-derived class called MyUserControl that is
able to persist and subsequently reload its state. It exposes two methods as
follows:

public void Serialize(Strea m s);
public void Deserialize(Str eam s);

Within the MyUserControl class, there is a field of type MyInnerClass
which is defined as follows:

private MyInnerClass MyInner=null;

...where MyInnerClass is defined as:

[Serializable()]
private class MyInnerClass{
public int Field;
public String Str;
}

When MyUserControl.S erialize() is called, 'MyInner' might be null or it
might not be. When it's not null, serialization takes place properly. When
it *is* null, serialization fails with an exception because
BinaryFormatter .Serialize() expects a non-null parameter.

What is the appropriate design pattern to use when serializing fields
which might be null? I'd like to avoid having to serialize an extra 'bool'
flag that indicates whether the field is null or not. Any ideas?

Thanks in advance,

David
Nov 15 '05 #1
5 24717

Hi David,

Are your
public void Serialize(Strea m s);
public void Deserialize(Str eam s);
implement by your self?
Are them invoke BinaryFormatter .Serialize(Stre am s, Object o) and
BinaryFormatter .Serialize(Stre am s)?
You use the default serialization or customer your own behavior?

I have tried the default serialization of binaryformatter and it works well
with innerclass null.
I did not find the constrain that the BinaryFormatter .Serialize can only
serialize non-null object.

If you defined your own way of serialization, can you show the code to me ?

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "David Sworder" <ds******@cts.c om>
| Subject: How to Serialize 'null'?
| Date: Wed, 20 Aug 2003 16:28:54 -0700
| Lines: 36
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <uN************ **@TK2MSFTNGP12 .phx.gbl>
| Newsgroups:
microsoft.publi c.dotnet.genera l,microsoft.pub lic.dotnet.lang uages.csharp
| NNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl
microsoft.publi c.dotnet.langua ges.csharp:1779 63
microsoft.publi c.dotnet.genera l:105256
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
|
| Hi,
|
| I've created a UserControl-derived class called MyUserControl that is
| able to persist and subsequently reload its state. It exposes two methods
as
| follows:
|
| public void Serialize(Strea m s);
| public void Deserialize(Str eam s);
|
| Within the MyUserControl class, there is a field of type MyInnerClass
| which is defined as follows:
|
| private MyInnerClass MyInner=null;
|
| ...where MyInnerClass is defined as:
|
| [Serializable()]
| private class MyInnerClass{
| public int Field;
| public String Str;
| }
|
| When MyUserControl.S erialize() is called, 'MyInner' might be null or
it
| might not be. When it's not null, serialization takes place properly. When
| it *is* null, serialization fails with an exception because
| BinaryFormatter .Serialize() expects a non-null parameter.
|
| What is the appropriate design pattern to use when serializing fields
| which might be null? I'd like to avoid having to serialize an extra 'bool'
| flag that indicates whether the field is null or not. Any ideas?
|
| Thanks in advance,
|
| David
|
|
|

Nov 15 '05 #2
Hi Jeffrey,

Yes, I implemented Serialize() and Deserialize() myself. These functions
eventually invoke the appropriate methods on the BinaryFormatter class. All
classes to be serialized are marked with the [Serializable()] attribute.
They do NOT implement ISerializable. The problem is occurring because
BinaryFormatter .Serialize() does not accept a null argument for the second
parameter. In other words, the top of the object graph cannot be null. You
can reproduce the error as follows:

ArrayList al=null;
new BinaryFormatter ().Serialize(st ream,al);

where 'stream' is a reference to a stream. In this trivial example, I'm
trying to serialize an ArrayList whose value is null. The exception thrown
is:

System.Argument NullException: Object Graph cannot be null.
Parameter name: graph

I can think of many different workarounds for this problem, but it seems
like the BinaryFormatter should be able to serialize a null field. If not,
what design pattern is typically used to circumvent this limitation. I
suppose the best approach would be to serialize a bit flag of some sort that
indicates which fields are null and therefore won't be written to the
stream.

Thanks again,

David
"Jeffrey Tan[MSFT]" <v-*****@online.mi crosoft.com> wrote in message
news:bl******** ******@cpmsftng xa06.phx.gbl...

Hi David,

Are your
public void Serialize(Strea m s);
public void Deserialize(Str eam s);
implement by your self?
Are them invoke BinaryFormatter .Serialize(Stre am s, Object o) and
BinaryFormatter .Serialize(Stre am s)?
You use the default serialization or customer your own behavior?

I have tried the default serialization of binaryformatter and it works well with innerclass null.
I did not find the constrain that the BinaryFormatter .Serialize can only
serialize non-null object.

If you defined your own way of serialization, can you show the code to me ?
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
| From: "David Sworder" <ds******@cts.c om>
| Subject: How to Serialize 'null'?
| Date: Wed, 20 Aug 2003 16:28:54 -0700
| Lines: 36
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <uN************ **@TK2MSFTNGP12 .phx.gbl>
| Newsgroups:
microsoft.publi c.dotnet.genera l,microsoft.pub lic.dotnet.lang uages.csharp
| NNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl
microsoft.publi c.dotnet.langua ges.csharp:1779 63
microsoft.publi c.dotnet.genera l:105256
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
|
| Hi,
|
| I've created a UserControl-derived class called MyUserControl that is | able to persist and subsequently reload its state. It exposes two methods as
| follows:
|
| public void Serialize(Strea m s);
| public void Deserialize(Str eam s);
|
| Within the MyUserControl class, there is a field of type MyInnerClass | which is defined as follows:
|
| private MyInnerClass MyInner=null;
|
| ...where MyInnerClass is defined as:
|
| [Serializable()]
| private class MyInnerClass{
| public int Field;
| public String Str;
| }
|
| When MyUserControl.S erialize() is called, 'MyInner' might be null or
it
| might not be. When it's not null, serialization takes place properly. When | it *is* null, serialization fails with an exception because
| BinaryFormatter .Serialize() expects a non-null parameter.
|
| What is the appropriate design pattern to use when serializing fields | which might be null? I'd like to avoid having to serialize an extra 'bool' | flag that indicates whether the field is null or not. Any ideas?
|
| Thanks in advance,
|
| David
|
|
|

Nov 15 '05 #3
David,
PMFJI: I too get an exception in VS.NET 2003, with an outer object as null.

However I question the 'need' for workarounds on serializing the outer most
objects. Isn't having a work around to serialize the outer most object =
null, like saying: I do not have a Word document open/created yet, but I
need to be able to save the Word document? Note I am saying I do not have a
Word document, as opposed to having an empty Word document. An empty Word
document I can save...

Although you are saying MyInnerClass, is it really your outer class for the
serialization purposes?

If you let the formatter serialize the inner classes while it is serializing
the outer class you do not need 'work arounds' there either.

If I have
| [Serializable()]
| private class MyInnerClass{
| public int Field;
| public String Str;
| }
Elsewhere I would also have: | [Serializable()]
| private class MyOuterClass{
| public MyInnerClass inner = null;
| }
I would have an instance of MyOuterClass which I was attempting to
serialize.

I would use:
MyOuterClass graph = new MyOuterClass();
new BinaryFormatter ().Serialize(st ream, graph);
The formatter, would serialize the MyOuterClass, while serializing
MyOuterClass, it will serialize the MyOuterClass.in ner field, seeing as the
field is null, no inner object will be serialized. If MyOuterClass.in ner had
an object reference, that object would be serialized.

The above is not intended as a work around, I'm explaining how I understand
serialization works.

Just confused & curious about what you are attempting as I am currently
working on serializing my objects.

Hope this helps
Jay

"David Sworder" <ds******@cts.c om> wrote in message
news:Oe******** *****@tk2msftng p13.phx.gbl... Hi Jeffrey,

Yes, I implemented Serialize() and Deserialize() myself. These functions eventually invoke the appropriate methods on the BinaryFormatter class. All classes to be serialized are marked with the [Serializable()] attribute.
They do NOT implement ISerializable. The problem is occurring because
BinaryFormatter .Serialize() does not accept a null argument for the second
parameter. In other words, the top of the object graph cannot be null. You
can reproduce the error as follows:

ArrayList al=null;
new BinaryFormatter ().Serialize(st ream,al);

where 'stream' is a reference to a stream. In this trivial example, I'm trying to serialize an ArrayList whose value is null. The exception thrown
is:

System.Argument NullException: Object Graph cannot be null.
Parameter name: graph

I can think of many different workarounds for this problem, but it seems like the BinaryFormatter should be able to serialize a null field. If not,
what design pattern is typically used to circumvent this limitation. I
suppose the best approach would be to serialize a bit flag of some sort that indicates which fields are null and therefore won't be written to the
stream.

Thanks again,

David
"Jeffrey Tan[MSFT]" <v-*****@online.mi crosoft.com> wrote in message
news:bl******** ******@cpmsftng xa06.phx.gbl...

Hi David,

Are your
public void Serialize(Strea m s);
public void Deserialize(Str eam s);
implement by your self?
Are them invoke BinaryFormatter .Serialize(Stre am s, Object o) and
BinaryFormatter .Serialize(Stre am s)?
You use the default serialization or customer your own behavior?

I have tried the default serialization of binaryformatter and it works well
with innerclass null.
I did not find the constrain that the BinaryFormatter .Serialize can only
serialize non-null object.

If you defined your own way of serialization, can you show the code to

me ?

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no

rights.
--------------------
| From: "David Sworder" <ds******@cts.c om>
| Subject: How to Serialize 'null'?
| Date: Wed, 20 Aug 2003 16:28:54 -0700
| Lines: 36
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <uN************ **@TK2MSFTNGP12 .phx.gbl>
| Newsgroups:
microsoft.publi c.dotnet.genera l,microsoft.pub lic.dotnet.lang uages.csharp
| NNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl
microsoft.publi c.dotnet.langua ges.csharp:1779 63
microsoft.publi c.dotnet.genera l:105256
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
|
| Hi,
|
| I've created a UserControl-derived class called MyUserControl that

is
| able to persist and subsequently reload its state. It exposes two

methods
as
| follows:
|
| public void Serialize(Strea m s);
| public void Deserialize(Str eam s);
|
| Within the MyUserControl class, there is a field of type

MyInnerClass
| which is defined as follows:
|
| private MyInnerClass MyInner=null;
|
| ...where MyInnerClass is defined as:
|
| [Serializable()]
| private class MyInnerClass{
| public int Field;
| public String Str;
| }
|
| When MyUserControl.S erialize() is called, 'MyInner' might be null or it
| might not be. When it's not null, serialization takes place properly.

When
| it *is* null, serialization fails with an exception because
| BinaryFormatter .Serialize() expects a non-null parameter.
|
| What is the appropriate design pattern to use when serializing

fields
| which might be null? I'd like to avoid having to serialize an extra

'bool'
| flag that indicates whether the field is null or not. Any ideas?
|
| Thanks in advance,
|
| David
|
|
|


Nov 15 '05 #4
Hi Jay,

Thanks for your great comments.

Here is the situation: Recall that I have a UserControl-derived class.
Users of this class need to be able to persist the state of the control.
This is done via a Serialize() method that I expose on my control. The user
may close/open the app numerous times and at some point it's possible that
the app may need to create a new instance of my UserControl-derived class
and restore it to it's given state as specified by the information persisted
in the file. This is done via the Deserialize() method exposed by my
control.

Note that I'm *not* trying to serialize the control itself. This would
make no sense [you can't serialize the window handles, etc]. I'm just trying
to serialize the state. So, let's look at my Serialize() function. It starts
off like this:

private Serialize(Strea m stream){
BinaryFormatter bf=new BinaryFormatter ();
bf.Serialize(st ream,innerObjec t);
bf.Serialize(st ream,someObject );
}

Note that one or both of 'innerObject' and 'someObject' might be null or
non-null. If one (or more) of the objects is null, I need to record that
"nullness" to the stream so that when my Deserialize() method is called, the
null-ness is restored. Does this make sense? The following call:

bf.Serialize(st ream,innerObjec t);

will throw an exception if 'innerObject' is null. See the problem?
Perhaps you're asking: If 'innerObject' is null, why serialize it at all?
Why not just use an 'if' condition to avoid serialization if 'innerObject'
is null? But that approach would corrupt the stream because my Deserialize()
method would have no idea whether or not to attempt deserialization of
"innerObjec t." That's why I'm considering the approach of serializing some
bit flags into the stream as a reminder at Deserialize-time of whether or
not certain objects should be deserialized or set to null.

I hope this makes sense.

David

"Jay B. Harlow [MVP - Outlook]" <Ja********@ema il.msn.com> wrote in message
news:uP******** ********@TK2MSF TNGP12.phx.gbl. ..
David,
PMFJI: I too get an exception in VS.NET 2003, with an outer object as null.
However I question the 'need' for workarounds on serializing the outer most objects. Isn't having a work around to serialize the outer most object =
null, like saying: I do not have a Word document open/created yet, but I
need to be able to save the Word document? Note I am saying I do not have a Word document, as opposed to having an empty Word document. An empty Word
document I can save...

Although you are saying MyInnerClass, is it really your outer class for the serialization purposes?

If you let the formatter serialize the inner classes while it is serializing the outer class you do not need 'work arounds' there either.

If I have
| [Serializable()]
| private class MyInnerClass{
| public int Field;
| public String Str;
| }
Elsewhere I would also have: | [Serializable()]
| private class MyOuterClass{
| public MyInnerClass inner = null;
| }
I would have an instance of MyOuterClass which I was attempting to
serialize.

I would use:
MyOuterClass graph = new MyOuterClass();
new BinaryFormatter ().Serialize(st ream, graph);
The formatter, would serialize the MyOuterClass, while serializing
MyOuterClass, it will serialize the MyOuterClass.in ner field, seeing as

the field is null, no inner object will be serialized. If MyOuterClass.in ner had an object reference, that object would be serialized.

The above is not intended as a work around, I'm explaining how I understand serialization works.

Just confused & curious about what you are attempting as I am currently
working on serializing my objects.

Hope this helps
Jay

"David Sworder" <ds******@cts.c om> wrote in message
news:Oe******** *****@tk2msftng p13.phx.gbl...
Hi Jeffrey,

Yes, I implemented Serialize() and Deserialize() myself. These functions
eventually invoke the appropriate methods on the BinaryFormatter class.

All
classes to be serialized are marked with the [Serializable()] attribute.
They do NOT implement ISerializable. The problem is occurring because
BinaryFormatter .Serialize() does not accept a null argument for the second parameter. In other words, the top of the object graph cannot be null. You can reproduce the error as follows:

ArrayList al=null;
new BinaryFormatter ().Serialize(st ream,al);

where 'stream' is a reference to a stream. In this trivial example,

I'm
trying to serialize an ArrayList whose value is null. The exception thrown is:

System.Argument NullException: Object Graph cannot be null.
Parameter name: graph

I can think of many different workarounds for this problem, but it

seems
like the BinaryFormatter should be able to serialize a null field. If not, what design pattern is typically used to circumvent this limitation. I
suppose the best approach would be to serialize a bit flag of some sort

that
indicates which fields are null and therefore won't be written to the
stream.

Thanks again,

David
"Jeffrey Tan[MSFT]" <v-*****@online.mi crosoft.com> wrote in message
news:bl******** ******@cpmsftng xa06.phx.gbl...

Hi David,

Are your
public void Serialize(Strea m s);
public void Deserialize(Str eam s);
implement by your self?
Are them invoke BinaryFormatter .Serialize(Stre am s, Object o) and
BinaryFormatter .Serialize(Stre am s)?
You use the default serialization or customer your own behavior?

I have tried the default serialization of binaryformatter and it works

well
with innerclass null.
I did not find the constrain that the BinaryFormatter .Serialize can only serialize non-null object.

If you defined your own way of serialization, can you show the code to me
?

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no

rights.
--------------------
| From: "David Sworder" <ds******@cts.c om>
| Subject: How to Serialize 'null'?
| Date: Wed, 20 Aug 2003 16:28:54 -0700
| Lines: 36
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <uN************ **@TK2MSFTNGP12 .phx.gbl>
| Newsgroups:
microsoft.publi c.dotnet.genera l,microsoft.pub lic.dotnet.lang uages.csharp | NNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213
| Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl
| Xref: cpmsftngxa06.ph x.gbl
microsoft.publi c.dotnet.langua ges.csharp:1779 63
microsoft.publi c.dotnet.genera l:105256
| X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
|
| Hi,
|
| I've created a UserControl-derived class called MyUserControl
that is
| able to persist and subsequently reload its state. It exposes two

methods
as
| follows:
|
| public void Serialize(Strea m s);
| public void Deserialize(Str eam s);
|
| Within the MyUserControl class, there is a field of type

MyInnerClass
| which is defined as follows:
|
| private MyInnerClass MyInner=null;
|
| ...where MyInnerClass is defined as:
|
| [Serializable()]
| private class MyInnerClass{
| public int Field;
| public String Str;
| }
|
| When MyUserControl.S erialize() is called, 'MyInner' might be
null or it
| might not be. When it's not null, serialization takes place

properly. When
| it *is* null, serialization fails with an exception because
| BinaryFormatter .Serialize() expects a non-null parameter.
|
| What is the appropriate design pattern to use when serializing

fields
| which might be null? I'd like to avoid having to serialize an extra

'bool'
| flag that indicates whether the field is null or not. Any ideas?
|
| Thanks in advance,
|
| David
|
|
|



Nov 15 '05 #5
David,
to serialize the state. So, let's look at my Serialize() function. It starts off like this:

private Serialize(Strea m stream){
BinaryFormatter bf=new BinaryFormatter ();
bf.Serialize(st ream,innerObjec t);
bf.Serialize(st ream,someObject );
} Actually it would help if we saw where it really started, where the stream
itself is created. :-)

I suspect where it really starts, you create a stream object, then you call
Serialize for each of your UserControls. Correct?

Causing multiple serializations to the same stream. Correct?

Do you always call each user control in same exact same order when you
serialize & deserialize them? If you don't you may have problems later...

Although it makes sense in the context of your design, I'm not convinced
that BinaryFormatter .Serialize should accept a null for the graph either.

I also do not think you need to find a 'workaround' for
BinaryFormatter .Serialize not accepting a null.

Which brings us to your design, not that your design is flawed or anything.
Its simple straight forward, easy to follow. Which is a good thing.

When you use someObject & innerObject in your user controls are you checking
for null each time you use them?

I'm having two somewhat completely different thoughts here, neither of which
may really work for you.

1. Consider using a Special Case Pattern for someObject & innerObject.
http://www.martinfowler.com/eaaCatalog/specialCase.html

Instead of allowing null to be stored in these variables and checking them
for null before each use. Have a NullObject, an object that derives from the
MyInnerClass (or same base class) that returns the same values you would
have used if the variable was null. Your serialization logic can then stay
put. NullObject could be as simple as a singleton in the MyInnerClass
similar to String.Empty. However on deserialization you then need to worry
about getting back to the specific singleton. The IObjectReferenc e interface
is useful in this regard. If NullObject was its own class derived from
MyInnerClass this would not be as big a problem, even then I would make this
new class a singleton.
http://msdn.microsoft.com/library/de...ClassTopic.asp

2. Consider having the Serialize & Deserialize methods accept a HashTable
(or something) instead. Adding or Getting someObject & innerObject from the
HashTable. I would consider using Control.Name as part of the key. Then
where the Stream is created, I would create a HashTable call all the
UserControl.Ser ialize methods, create the stream, serialize the hashtable,
close the stream.

Hope this helps
Jay

"David Sworder" <ds******@cts.c om> wrote in message
news:OG******** ******@TK2MSFTN GP10.phx.gbl... Hi Jay,

Thanks for your great comments.

Here is the situation: Recall that I have a UserControl-derived class.
Users of this class need to be able to persist the state of the control.
This is done via a Serialize() method that I expose on my control. The user may close/open the app numerous times and at some point it's possible that
the app may need to create a new instance of my UserControl-derived class
and restore it to it's given state as specified by the information persisted in the file. This is done via the Deserialize() method exposed by my
control.

Note that I'm *not* trying to serialize the control itself. This would
make no sense [you can't serialize the window handles, etc]. I'm just trying to serialize the state. So, let's look at my Serialize() function. It starts off like this:

private Serialize(Strea m stream){
BinaryFormatter bf=new BinaryFormatter ();
bf.Serialize(st ream,innerObjec t);
bf.Serialize(st ream,someObject );
}

Note that one or both of 'innerObject' and 'someObject' might be null or non-null. If one (or more) of the objects is null, I need to record that
"nullness" to the stream so that when my Deserialize() method is called, the null-ness is restored. Does this make sense? The following call:

bf.Serialize(st ream,innerObjec t);

will throw an exception if 'innerObject' is null. See the problem?
Perhaps you're asking: If 'innerObject' is null, why serialize it at all?
Why not just use an 'if' condition to avoid serialization if 'innerObject'
is null? But that approach would corrupt the stream because my Deserialize() method would have no idea whether or not to attempt deserialization of
"innerObjec t." That's why I'm considering the approach of serializing some
bit flags into the stream as a reminder at Deserialize-time of whether or
not certain objects should be deserialized or set to null.

I hope this makes sense.

David

"Jay B. Harlow [MVP - Outlook]" <Ja********@ema il.msn.com> wrote in message news:uP******** ********@TK2MSF TNGP12.phx.gbl. ..
David,
PMFJI: I too get an exception in VS.NET 2003, with an outer object as null.

However I question the 'need' for workarounds on serializing the outer

most
objects. Isn't having a work around to serialize the outer most object =
null, like saying: I do not have a Word document open/created yet, but I
need to be able to save the Word document? Note I am saying I do not have a
Word document, as opposed to having an empty Word document. An empty Word document I can save...

Although you are saying MyInnerClass, is it really your outer class for

the
serialization purposes?

If you let the formatter serialize the inner classes while it is

serializing
the outer class you do not need 'work arounds' there either.

If I have
> | [Serializable()]
> | private class MyInnerClass{
> | public int Field;
> | public String Str;
> | }


Elsewhere I would also have:
> | [Serializable()]
> | private class MyOuterClass{
> | public MyInnerClass inner = null;
> | }


I would have an instance of MyOuterClass which I was attempting to
serialize.

I would use:
MyOuterClass graph = new MyOuterClass();
new BinaryFormatter ().Serialize(st ream, graph);


The formatter, would serialize the MyOuterClass, while serializing
MyOuterClass, it will serialize the MyOuterClass.in ner field, seeing as

the
field is null, no inner object will be serialized. If MyOuterClass.in ner

had
an object reference, that object would be serialized.

The above is not intended as a work around, I'm explaining how I

understand
serialization works.

Just confused & curious about what you are attempting as I am currently
working on serializing my objects.

Hope this helps
Jay

"David Sworder" <ds******@cts.c om> wrote in message
news:Oe******** *****@tk2msftng p13.phx.gbl...
Hi Jeffrey,

Yes, I implemented Serialize() and Deserialize() myself. These

functions
eventually invoke the appropriate methods on the BinaryFormatter class.
All
classes to be serialized are marked with the [Serializable()]
attribute. They do NOT implement ISerializable. The problem is occurring because
BinaryFormatter .Serialize() does not accept a null argument for the

second parameter. In other words, the top of the object graph cannot be null. You can reproduce the error as follows:

ArrayList al=null;
new BinaryFormatter ().Serialize(st ream,al);

where 'stream' is a reference to a stream. In this trivial example, I'm
trying to serialize an ArrayList whose value is null. The exception thrown is:

System.Argument NullException: Object Graph cannot be null.
Parameter name: graph

I can think of many different workarounds for this problem, but it

seems
like the BinaryFormatter should be able to serialize a null field. If not, what design pattern is typically used to circumvent this limitation. I
suppose the best approach would be to serialize a bit flag of some
sort
that
indicates which fields are null and therefore won't be written to the
stream.

Thanks again,

David
"Jeffrey Tan[MSFT]" <v-*****@online.mi crosoft.com> wrote in message
news:bl******** ******@cpmsftng xa06.phx.gbl...
>
> Hi David,
>
> Are your
> public void Serialize(Strea m s);
> public void Deserialize(Str eam s);
> implement by your self?
> Are them invoke BinaryFormatter .Serialize(Stre am s, Object o) and
> BinaryFormatter .Serialize(Stre am s)?
> You use the default serialization or customer your own behavior?
>
> I have tried the default serialization of binaryformatter and it
works well
> with innerclass null.
> I did not find the constrain that the BinaryFormatter .Serialize can

only > serialize non-null object.
>
> If you defined your own way of serialization, can you show the code to me
?
>
> Best regards,
> Jeffrey Tan
> Microsoft Online Partner Support
> Get Secure! - www.microsoft.com/security
> This posting is provided "as is" with no warranties and confers no

rights.
>
> --------------------
> | From: "David Sworder" <ds******@cts.c om>
> | Subject: How to Serialize 'null'?
> | Date: Wed, 20 Aug 2003 16:28:54 -0700
> | Lines: 36
> | X-Priority: 3
> | X-MSMail-Priority: Normal
> | X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
> | X-MIMEOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
> | Message-ID: <uN************ **@TK2MSFTNGP12 .phx.gbl>
> | Newsgroups:
> microsoft.publi c.dotnet.genera l,microsoft.pub lic.dotnet.lang uages.csharp > | NNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213
> | Path:
cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!TK2 MSFTNGP12.phx.g bl > | Xref: cpmsftngxa06.ph x.gbl
> microsoft.publi c.dotnet.langua ges.csharp:1779 63
> microsoft.publi c.dotnet.genera l:105256
> | X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.csharp
> |
> | Hi,
> |
> | I've created a UserControl-derived class called MyUserControl

that is
> | able to persist and subsequently reload its state. It exposes two
methods
> as
> | follows:
> |
> | public void Serialize(Strea m s);
> | public void Deserialize(Str eam s);
> |
> | Within the MyUserControl class, there is a field of type
MyInnerClass
> | which is defined as follows:
> |
> | private MyInnerClass MyInner=null;
> |
> | ...where MyInnerClass is defined as:
> |
> | [Serializable()]
> | private class MyInnerClass{
> | public int Field;
> | public String Str;
> | }
> |
> | When MyUserControl.S erialize() is called, 'MyInner' might be null
or
> it
> | might not be. When it's not null, serialization takes place

properly. When
> | it *is* null, serialization fails with an exception because
> | BinaryFormatter .Serialize() expects a non-null parameter.
> |
> | What is the appropriate design pattern to use when serializing
fields
> | which might be null? I'd like to avoid having to serialize an extra 'bool'
> | flag that indicates whether the field is null or not. Any ideas?
> |
> | Thanks in advance,
> |
> | David
> |
> |
> |
>



Nov 15 '05 #6

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

Similar topics

2
2274
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream s); public void Deserialize(Stream s); Within the MyUserControl class, there is a field of type MyInnerClass
5
2551
by: andrewcw | last post by:
I have an object to serialize. TextWriter writer = new StreamWriter("test.xml"); serializer.Serialize(writer,obj); writer.Close(); but below does not, why ?? I have a file that I will have exclusively opened & I use the stream for that operation. THANKS { I am tring to push the object back into the stream )
10
4158
by: Dan | last post by:
All I Am Attempting To Serialize An Object To An XML File. Here Is The Code For That public string SaveNewSurvey( MutualSurveyObject mso_TempObject, int i_JobID ) { string s_RootFileName; string s_FinalFileName; try
0
3442
by: meh | last post by:
Still have not been able to convert this. More importently.....Is this sample going about it the right way??? tia meh Here is the vb code.........I keep looking at older vb.net projects to try and figure out how to teach myself C# based on my old vb code but it's been more difficult than I first thought. I'm finding that it would be better if didn't know any previous programming language.
3
2387
by: Jerry | last post by:
Hi, I have a class like the following: class A { private B _b; A (B b) { _b = b; } ...
3
15174
by: Mirek Endys | last post by:
What is the best way to serialize object, that contains data in interfaces. Simple. I have instance of my object, that contains data in interfaces. What is the best way to XMLSerialize and deserialize this object? Thanks.
18
2350
by: yusuf | last post by:
I basically want to be able to store and retrieve a tree structure in greasemonkey. Unfortunately the GM_setValue and GM_getValue functions only take string key value pairs. Is there a native javascript function that will serialize the object so that it can be stored and retrieved as strings? Thanks.
1
7253
by: job | last post by:
how is it possible to serialize/de-serialize a SqlCommand?
5
3332
by: =?Utf-8?B?Qm9uaQ==?= | last post by:
Hi, I have a class to serialize. This class has come properties like this one private myObject _listOf; public myObject listOf { get { if(_listOf==null) { _listOf=myClass.getMyObjectList();
0
8889
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9257
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9179
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8099
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.