Creting a custom collection class
Hello all,
I am writing an application that will utilise a set of my own classes.
I want to create another class that acts as a collection of my class.
For example I could have a class "User".
At some point in my application I may need to instanciate x objects of class "User".
I would like to create a class called "UserCollection" which could then be used to group all x "User" objects so that when neccesary I could itterate through the "UserCollection" object like.....
UserCollection myUsers = new UserColection();
User myUser1 = new User(a,b);
myUsers.Add(myUser1);
User myUser2 = new User(c,d);
myUsers.Add(myUser2);
User myUser3 = new User(e,f);
myUsers.Add(myUser3);
User myUser4 = new User(g,h);
myUsers.Add(myUser4);
....
User myUser13 = new User(y,z);
myUsers.Add(myUser13);
foreach(User currentUser in myUsers)
{
currentUser.callMethod1();
}
Is there sample code around for doing this?
Thanks
Answers (2)
0
Thanks for your help - will have a look and see what I can come up with.
I do still want to use my own collection class because I want to extend the collection class to include more properties specific to the purpose of the class. The example I gave is just an example I made up on the spot.
Thanks Again
0
For the example that you have given a simple ArrayList would suffice but if you do want to crate your own collection class you could inherited from CollectionBase. The online MSDN documentation provides an example using this class:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemCollectionsCollectionBaseClassTopic.asp