Lists<> of custom classes

A

Andrew Bullock

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/books/csbasics/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;
}
}
}
 
A

Andrew Bullock

<snip>

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

Sorry


Andrew
 
S

Stephany Young

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'.
 
A

Andrew Bullock

Stephany said:
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
 
S

SP

Andrew Bullock said:
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
 

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