custom collection

O

Oterox

Hi!
i have a class "user" with 3 properties: userIid, userName, userEmail and
another class that is a collection of users "userCollection":
namespace meta01.colecciones
{

public class userCollection: System.Collections.CollectionBase
{
public userCollection()
{
}

public user this[ int index ]
{
get
{
return( (user) List[index] );
}
set
{
List[index] = value;
}
}

public void add(user oUser)
{
List.Add(oUser);
}

public void remove(user value)
{
List.Remove(value);
}
public user getUser(int i)
{
return (user)List;
}
public int IndexOf( user value )
{
return (List.IndexOf(value)) ;
}

public void Insert(int index, user value)
{

this.List.Insert(index, value);

}

}
}
In my webform i create some users like this:
oUserCollection.add(new user(1,"user1","(e-mail address removed)"));
oUserCollection.add(new user(2,"user2","(e-mail address removed)"));
oUserCollection.add(new user(3,"user3","(e-mail address removed)"));
oUserCollection.add(new user(4,"user4","(e-mail address removed)"));


I can list the contents of the collection but how can i get an user by its
userID.
If i want the details of user1 how can i get them???
There's a better way of doing this??

Thank you very much.
 
M

mortb

I usually do it like this:

public getUserById(int userId)
{
foreach(user userobject in List)
{
if(userobject.userId = userId) return user;
}
return null;
}

Optimization?
If you have large collections of users and you do this operation often you
could base the usercollection on a hash tabel and use the userid as key.
Looking up a user would probably then be more efficient.

cheers,
mortb
 

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