473,416 Members | 1,585 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,416 software developers and data experts.

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(Stream s);
public void Deserialize(Stream 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.Serialize() 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 24645

Hi David,

Are your
public void Serialize(Stream s);
public void Deserialize(Stream s);
implement by your self?
Are them invoke BinaryFormatter.Serialize(Stream s, Object o) and
BinaryFormatter.Serialize(Stream 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.com>
| 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.public.dotnet.general,microsoft.public.d otnet.languages.csharp
| NNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.languages.csharp:177963
microsoft.public.dotnet.general:105256
| X-Tomcat-NG: microsoft.public.dotnet.languages.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(Stream s);
| public void Deserialize(Stream 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.Serialize() 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(stream,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.ArgumentNullException: 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.microsoft.com> wrote in message
news:bl**************@cpmsftngxa06.phx.gbl...

Hi David,

Are your
public void Serialize(Stream s);
public void Deserialize(Stream s);
implement by your self?
Are them invoke BinaryFormatter.Serialize(Stream s, Object o) and
BinaryFormatter.Serialize(Stream 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.com>
| 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.public.dotnet.general,microsoft.public.d otnet.languages.csharp
| NNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.languages.csharp:177963
microsoft.public.dotnet.general:105256
| X-Tomcat-NG: microsoft.public.dotnet.languages.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(Stream s);
| public void Deserialize(Stream 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.Serialize() 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(stream, graph);
The formatter, would serialize the MyOuterClass, while serializing
MyOuterClass, it will serialize the MyOuterClass.inner field, seeing as the
field is null, no inner object will be serialized. If MyOuterClass.inner 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.com> wrote in message
news:Oe*************@tk2msftngp13.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(stream,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.ArgumentNullException: 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.microsoft.com> wrote in message
news:bl**************@cpmsftngxa06.phx.gbl...

Hi David,

Are your
public void Serialize(Stream s);
public void Deserialize(Stream s);
implement by your self?
Are them invoke BinaryFormatter.Serialize(Stream s, Object o) and
BinaryFormatter.Serialize(Stream 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.com>
| 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.public.dotnet.general,microsoft.public.d otnet.languages.csharp
| NNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.languages.csharp:177963
microsoft.public.dotnet.general:105256
| X-Tomcat-NG: microsoft.public.dotnet.languages.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(Stream s);
| public void Deserialize(Stream 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.Serialize() 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(Stream stream){
BinaryFormatter bf=new BinaryFormatter();
bf.Serialize(stream,innerObject);
bf.Serialize(stream,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(stream,innerObject);

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
"innerObject." 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********@email.msn.com> wrote in message
news:uP****************@TK2MSFTNGP12.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(stream, graph);
The formatter, would serialize the MyOuterClass, while serializing
MyOuterClass, it will serialize the MyOuterClass.inner field, seeing as

the field is null, no inner object will be serialized. If MyOuterClass.inner 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.com> wrote in message
news:Oe*************@tk2msftngp13.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(stream,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.ArgumentNullException: 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.microsoft.com> wrote in message
news:bl**************@cpmsftngxa06.phx.gbl...

Hi David,

Are your
public void Serialize(Stream s);
public void Deserialize(Stream s);
implement by your self?
Are them invoke BinaryFormatter.Serialize(Stream s, Object o) and
BinaryFormatter.Serialize(Stream 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.com>
| 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.public.dotnet.general,microsoft.public.d otnet.languages.csharp | NNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.languages.csharp:177963
microsoft.public.dotnet.general:105256
| X-Tomcat-NG: microsoft.public.dotnet.languages.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(Stream s);
| public void Deserialize(Stream 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.Serialize() 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(Stream stream){
BinaryFormatter bf=new BinaryFormatter();
bf.Serialize(stream,innerObject);
bf.Serialize(stream,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 IObjectReference 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.Serialize methods, create the stream, serialize the hashtable,
close the stream.

Hope this helps
Jay

"David Sworder" <ds******@cts.com> wrote in message
news:OG**************@TK2MSFTNGP10.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(Stream stream){
BinaryFormatter bf=new BinaryFormatter();
bf.Serialize(stream,innerObject);
bf.Serialize(stream,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(stream,innerObject);

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
"innerObject." 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********@email.msn.com> wrote in message news:uP****************@TK2MSFTNGP12.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(stream, graph);


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

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

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.com> wrote in message
news:Oe*************@tk2msftngp13.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(stream,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.ArgumentNullException: 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.microsoft.com> wrote in message
news:bl**************@cpmsftngxa06.phx.gbl...
>
> Hi David,
>
> Are your
> public void Serialize(Stream s);
> public void Deserialize(Stream s);
> implement by your self?
> Are them invoke BinaryFormatter.Serialize(Stream s, Object o) and
> BinaryFormatter.Serialize(Stream 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.com>
> | 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.public.dotnet.general,microsoft.public.d otnet.languages.csharp > | NNTP-Posting-Host: rrcs-west-66-27-51-213.biz.rr.com 66.27.51.213
> | Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP12.phx.gbl > | Xref: cpmsftngxa06.phx.gbl
> microsoft.public.dotnet.languages.csharp:177963
> microsoft.public.dotnet.general:105256
> | X-Tomcat-NG: microsoft.public.dotnet.languages.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(Stream s);
> | public void Deserialize(Stream 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.Serialize() 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
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...
5
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...
10
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;...
0
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...
3
by: Jerry | last post by:
Hi, I have a class like the following: class A { private B _b; A (B b) { _b = b; } ...
3
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...
18
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...
1
by: job | last post by:
how is it possible to serialize/de-serialize a SqlCommand?
5
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) {...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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
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
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...
0
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,...
0
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...

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.