Getting object by ID from collection?

B

Brett Romero

I have a class with a list:

public class PersonCollection<T>
{
public List<T> List = new List<T>();
....
}

Let's say I have added five Person objects to List. I add them by:

PersonCollection<Person> pc = new PersonCollection<Person>();
PersonCollection.List.Add(p1);

A Person object has a public PersonID property. Person object at index
of 2 (3rd in list) has PersonID = 1005. I can get this object by:

myPerson = PersonCollection.List[2];

I'd like instead to say:

myPerson = PersonCollection.List[1005];

However, I'm not sure how to do that without creating two list. How
can it be done so that I just set the Generic List<> type and pass the
constructor the field that it will search off of to retrieve objects in
the list?

Thanks,
Brett
 
O

Otis Mukinfus

Hi Brett,

I think what you are after is a Hashtable ;)

- Mark

Or qsort and binary search.
I have a class with a list:

public class PersonCollection<T>
{
public List<T> List = new List<T>();
....
}

Let's say I have added five Person objects to List. I add them by:

PersonCollection<Person> pc = new PersonCollection<Person>();
PersonCollection.List.Add(p1);

A Person object has a public PersonID property. Person object at index
of 2 (3rd in list) has PersonID = 1005. I can get this object by:

myPerson = PersonCollection.List[2];

I'd like instead to say:

myPerson = PersonCollection.List[1005];

However, I'm not sure how to do that without creating two list. How
can it be done so that I just set the Generic List<> type and pass the
constructor the field that it will search off of to retrieve objects in
the list?

Thanks,
Brett

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
B

Brett Romero

I'm not sure what a qsort is and HashTable doesn't allow typing.
However, this set me on the right path:

public class PersonCollection<Person>
{
public Dictionary<int, Person> PersonList = new Dictionary<int,
Person>();
public void AddPerson(int personID, Person p)
{
this.PersonList.Add(personID, p);
}
}

//running the code somewhere
mypc.AddPerson(p.PersonId, p);
p2 = hc.PersonList[p.PersonId];

In the above, I successful get p (person1) and assign to p2 (person2).
Very nice.

What good does typing my class as Person do here? Or, what is a
scenario that using the Person type on this class proves efficient?

Thanks,
Brett
 
J

Jon Skeet [C# MVP]

What good does typing my class as Person do here? Or, what is a
scenario that using the Person type on this class proves efficient?

It doesn't, in this case. Usually when you use generics you either make
a class generic, or make it derive from a generic class, possibly
specifying the concrete type parameter, eg:

public class PersonList : List<Person>

(I'm not suggesting you do that in this case, by the way.)

I just wouldn't make the class generic at all in this case. You can
still use Dictionary<int, Person> though.
 
B

Brett Romero

I thought about using

public class PersonList : List<Person>

which would let me run it this way:

mypc.Add(p.PersonId, p);
p2 = mypc[p.PersonId];

Meaning, I no longer explicitly reference the list.

Thanks on the clarification Jon,
Brett
 

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