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
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