471,350 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

Lists<> of custom classes

Hi,

I've made my own class (we'll call it myClass) to simply store some
data. I'm storing multiple instances myClass in a list:

List<myClass> myList = new List<myClass>();
myList.Add(new myClass(somedata));
myList.Add(new myClass(somemoredata));

I'm then iterating over myList using a for loop. To neaten my code i
decided to use a foreach, but of course, when i do that i get some
serialization error because myClass doesnt implement GetEnumerator.

I've read this:
http://www.vijaymukhi.com/documents/...ics/chap13.htm

but I'm still not very clear on how I need to modify myClass to enable
foreach-ing.

Can someone please advise me?

Thanks,

Andrew

Below is the code for myClass:
////////////

internal class myClass
{
public int X;
public int Y;

public myClass(int _x, int _y)
{
X = _x;
Y = _y;
}
}
}
Feb 18 '06 #1
4 1341
<snip>

Hmmmm... seems to be working now, and I haven't changed anything :s

Sorry
Andrew
Feb 18 '06 #2
Don't you just hate that!

I'd better offer you a job before anyone else does.

I'm always on the look out for someone who can correct problems in code
without changing anything.

It's a talent that most of us don't have.

Rather than 'I haven't changed anything', I'll wager that it is more a
matter of 'I don't know what I changed' or 'I don't know what I did
differently'.
"Andrew Bullock" <an*********************@ANDntlworldTHIS.com> wrote in
message news:MS*******************@newsfe4-win.ntli.net...
<snip>

Hmmmm... seems to be working now, and I haven't changed anything :s

Sorry
Andrew

Feb 18 '06 #3
Stephany Young wrote:
Don't you just hate that!

I'd better offer you a job before anyone else does.

I'm always on the look out for someone who can correct problems in code
without changing anything.

It's a talent that most of us don't have.

Rather than 'I haven't changed anything', I'll wager that it is more a
matter of 'I don't know what I changed' or 'I don't know what I did
differently'.


Nope, honestly I didn't change a thing!

I think its something to do with the debugger and breakpoints...

/investigates

Andrew
Feb 18 '06 #4
SP

"Andrew Bullock" <an*********************@ANDntlworldTHIS.com> wrote in
message news:WA*******************@newsfe6-win.ntli.net...
Hi,

I've made my own class (we'll call it myClass) to simply store some data.
I'm storing multiple instances myClass in a list:

List<myClass> myList = new List<myClass>();
myList.Add(new myClass(somedata));
myList.Add(new myClass(somemoredata));

I'm then iterating over myList using a for loop. To neaten my code i
decided to use a foreach, but of course, when i do that i get some
serialization error because myClass doesnt implement GetEnumerator.


It doesn't have to. The foreach is performed on the List<> not your class.

SP
Feb 19 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Dave | last post: by
4 posts views Thread by Peter Notebaert | last post: by
4 posts views Thread by Emre Guldogan | last post: by
2 posts views Thread by Robert Bravery | last post: by
1 post views Thread by Eran | last post: by
20 posts views Thread by martin-g | last post: by
3 posts views Thread by muquaddim | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.