Generics for custom collections

G

Guest

I am developing a new management system for my company, and with it have 3
layers of functionality written in VB.NET 2.0. In the business logic layer I
have a user class which contains all user information. I also would like to
put in a userCollection class for all my users. There are many other classes
like this on as well. However, what is the best way to do my collections? I
would like them so that a userCollection can only contain user items and so
on. I have seen that I could write my own collection class by inheriting
from the collection base. Would it be easier if I used generics instead?
The syntax is not correct but something like Private userCollection as
Collection(of Users) can this be done? If so, is it easier than developing
100+ seperate collection classes? How would I go about generics?
 
C

Chris Mullins

However, what is the best way to do my collections? I would like
them so that a userCollection can only contain user items and so
on.

Sounds like a job for Generics...
I have seen that I could write my own collection class by inheriting
from the collection base.

Ick. You may as well code in Perl, Cobol or Fortran if you're going to do
that...
Would it be easier if I used generics instead?

Heck yea.
The syntax is not correct but something like Private userCollection as
Collection(of Users) can this be done? If so, is it easier than
developing
100+ seperate collection classes? How would I go about generics?

The code for the generic collection looks like:

// The Custom Class
public class CustomClass
{
public string Name;
}

// The Custom Collection
public class CustomCollection : List<CustomClass>
{
// Add in any custom constructors you want here...
}
 
C

Chris Dunaway

That would be:

Public Class CustomClass
End Class

Public Class CustomCollection
Inherits List(Of CustomClass)

End Class
 

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