Problem when serializing class that implements IEnumerable

C

Craig Buchanan

I have a class called LineItems. It implements IEnumerable. It uses an
ArrayList to hold objects. When I try to serialize the object that contains
LineItems (an object called Order), I get an error that reads:

You must implement the Add(System.Object) method on LineItems because it
inherits from IEnumerable.

I've implemented GetEnumerator on the LineItems class. I also have two Add
methods.

What am I missing?

Thanks,

Craig Buchanan
 
V

Vladimir Sadov [MSFT]

Hi Craig,

I tried to follow your description and everything worked as expected. Can
you provide more deatails on what you are doing?

Thanks,
Vladimir [VB.Net team]


--------------------
| From: "Craig Buchanan" <[email protected]>
| Subject: Problem when serializing class that implements IEnumerable
| Date: Thu, 29 Jan 2004 09:51:35 -0600
| Lines: 17
| 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: <uS6t##[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.vb
| NNTP-Posting-Host: 152-41.dsl.scc.net 209.32.152.41
| Path:
cpmsftngxa07.phx.gbl!cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.
phx.gbl
| Xref: cpmsftngxa07.phx.gbl microsoft.public.dotnet.languages.vb:177166
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| I have a class called LineItems. It implements IEnumerable. It uses an
| ArrayList to hold objects. When I try to serialize the object that
contains
| LineItems (an object called Order), I get an error that reads:
|
| You must implement the Add(System.Object) method on LineItems because it
| inherits from IEnumerable.
|
| I've implemented GetEnumerator on the LineItems class. I also have two
Add
| methods.
|
| What am I missing?
|
| Thanks,
|
| Craig Buchanan
|
|
|
 
G

Guest

I've got the exact same problem

A strongly typed collection that implements IEnumerable. and is marked as serializable

Attempting to use the following on a consumer of the clas

Dim swWriter As New System.IO.StringWrite
Dim xsSerializer As New System.Xml.Serialization.XmlSerializer(GetType(DiscussionAreas)

gives the message
"You must implement the Add(System.Object) method on StrongTypes.DiscussionAreas because it inherits from IEnumerable.

The header of the class is as follow

Imports System.Xml.Serializatio

<XmlInclude(GetType(DiscussionAreas)), Serializable()>
Public Class DiscussionArea
Implements IEnumerabl

Certainly not inheriting...

Any ideas?
 
C

Charles Law

Hi Craig

What does you implementation of Add look like for the class DiscussionAreas?

Charles


Craig said:
I've got the exact same problem.

A strongly typed collection that implements IEnumerable. and is marked as serializable.

Attempting to use the following on a consumer of the class

Dim swWriter As New System.IO.StringWriter
Dim xsSerializer As New System.Xml.Serialization.XmlSerializer(GetType(DiscussionAreas))

gives the message
"You must implement the Add(System.Object) method on
StrongTypes.DiscussionAreas because it inherits from IEnumerable."
 
G

Guest

nothing special but it doesn't contain the system.object method..

Public Sub Add(ByVal DiscussionArea As DiscussionArea

It's confusing why it mentions inheritance. when clearly I'm implementing
 
C

Charles Law

Hi Craig

I suspect that if you implement it as

Public Sub Add(ByVal DiscussionArea As Object)

then the problem will go away. It suggests that when the serializer
reads/writes the file it sees the item as type Object and doesn't recognise
its real type.

Another minor point: I try to avoid naming variables and parameters with the
same name as classes. It avoids any confusion for the compiler and reader.

HTH

Charles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top