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

Cannot iterate through an object collection.

P: 59
Hey guys.
I'm building an object collection which will be a part of a Data Access Layer i am currently working on. However, i am having some issues iterating through a collection.
This is what i've done so far
1 - Create 'user' class which maps to the MySQL table. The class has basic setters and getters to set/get each field value.
2 - Created a collection class with simple add/remove/get methods.
3 - Created user collection class which extends my collection class described above. This user collection class overwrites addItem() method to achieve adding item of type 'user' to my user collection.
4 - Created collection iterator class which implements Iterator interface and overwrites its basic methods such as rewind/next/current and etc.
5 - Changed my collection class to implement IteratorAggregate interface.

At this point i should be able to use foreach() operator on my collection object but i can't. In fact, it doesn't even go into the foreach loop.

Here is the source: dowload

you can see i was testing foreach in the user collection class.

I appreciate any help i can get on this.
Dec 25 '07 #1
Share this Question
Share on Google+
3 Replies


pbmods
Expert 5K+
P: 5,821
Heya, Gasfusion.

foreach only works on public data members when used on a class instance.

Check out the ArrayObject class.
Dec 25 '07 #2

P: 59
I've recoded all classes and got it to work. Have no clue what it was that was wrong.
Thanks.
Dec 28 '07 #3

pbmods
Expert 5K+
P: 5,821
Heya, Gasfusion.

Glad to hear you got it working! Good luck with your project, and if you ever need anything, post back anytime :)
Dec 30 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.