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

How to Serialize 'null'?

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hello David,

I noticed that the issue was posted in several groups (places).
* I have added a reply to you at microsoft.public.dotnet.languages.csharp
If you have follow up questions, please post there and I will work with
you. Thanks.

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.general
|
| 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
|
|
|

Jul 19 '05 #2

P: n/a
Thanks Jeffery,

See my followup on the C# newsgroup.

David

"Jeffrey Tan[MSFT]" <v-*****@online.microsoft.com> wrote in message
news:C0**************@cpmsftngxa06.phx.gbl...
Hello David,

I noticed that the issue was posted in several groups (places).
* I have added a reply to you at microsoft.public.dotnet.languages.csharp
If you have follow up questions, please post there and I will work with
you. Thanks.

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.general
|
| 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
|
|
|

Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.