Collections question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,
I want to create a collection type: LineRuns containing type: LineRun. I
would like to implement the IEnumerable interface to be able to use foreach
against the LineRuns collection. The LineRuns class has a method called:
AddLineRun() which will create an instance of the LineRun class. How should
I do this?
 
See the section in MSDN on creating custom collections.

The principle is simple. Derive a class from CollectionBase and provide
strongly typed Add and Remove methods and an indexer.

Because CollectionBase already supports the right interfaces you can use
ForEach on a class derivedfrom it.

I suggest that you forget the AddLineRuns method in the LineRuns class and
simply create a suitable constructor so that you can do something like...

LineRunCollection.Add(new LineRun(x,y,xx,yy));

--
Bob Powell [MVP]
Visual C#, System.Drawing

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml
 
An easy way to implement this would be to inherit your own collections class
from System.Collections.CollectionBase, which would implement this for you.
Otherwise, you can inherit your own class from ICollection and implement
IEnumerable by calling GetEnumerator on your "inner" collection (the data
structure--ArrayList, etc.--in which you actually store your collection
items) in your implementation of IEnumerator.

Hope this helps.

Travis J. James
Butterfly Software Group, Inc.
 
Ok got it. Thanks.

Bob Powell said:
See the section in MSDN on creating custom collections.

The principle is simple. Derive a class from CollectionBase and provide
strongly typed Add and Remove methods and an indexer.

Because CollectionBase already supports the right interfaces you can use
ForEach on a class derivedfrom it.

I suggest that you forget the AddLineRuns method in the LineRuns class and
simply create a suitable constructor so that you can do something like...

LineRunCollection.Add(new LineRun(x,y,xx,yy));

--
Bob Powell [MVP]
Visual C#, System.Drawing

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

The GDI+ FAQ RSS feed: http://www.bobpowell.net/faqfeed.xml
Windows Forms Tips and Tricks RSS: http://www.bobpowell.net/tipstricks.xml
Bob's Blog: http://bobpowelldotnet.blogspot.com/atom.xml
 
Thank you. Your reply also helped me understand what an "inner" collection
is. I did not understand the MSDN examples properly before your reply.
 
Back
Top