Problem when serializing class that implements IEnumerable

  • Thread starter Thread starter Craig Buchanan
  • Start date Start date
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
 
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
|
|
|
 
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?
 
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."
 
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
 
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
 
Back
Top