Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enumerable classes

Status
Not open for further replies.

perryf

MIS
Nov 15, 2001
63
US
I have a general question on enumerable classes based on Chip's FAQ.

My source for studying C# describes the same scenario as Chip addresses. The is a slight difference:

Chip:
- The enumerated class is an additional class named as a plural of the items class (i.e. Song(s)).

Book:
- The enumerated class is a nested class contained within the main class (i.e. AlbumEnumerator class contained within Album. Album will function as a collection of Song objects).

Just out of curiousity, are there really any drawbacks to either method. Is it just personal preference.

Thanks,

Perry

 
You have to look at the name of the interface very carefully -- it threw me at first, too.

The Songs class implements the IEnumerable interface, and the nested class implements the IEnumerator interface.

Chip H.

BTW, thanks to whomever sent me email about my FAQ - you're right, I had a cut & paste error in the MoveNext method!
 
Oh, wait. I just re-read your message and I think I get what you're saying now.

Your book is saying that you can nest the declaration of the Songs class inside the Album class (and SongEnumerator inside that!). And yes, you can do that.

The difference is that in the book method, the Songs collection wouldn't be creatable outside an Album object, without a public method to act as a factory. In my case, you can create a Songs collection that exists outside an Album directly.

It really depends on how you anticipate the collection will be used. Six of one, half-dozen of the other.

Chip H.
 
Chip,

Thanks for taking the time to explain. Makes sense that your way is more flexible in a "just in case..." kind of way.

Thanks,

Perry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top