473,386 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Getting object by ID from collection?

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

Mar 14 '06 #1
5 1551
Hi Brett,

I think what you are after is a Hashtable ;)

- Mark

On Tue, 14 Mar 2006 08:41:53 +0800, Brett Romero <ac*****@cygen.com> wrote:
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


Mar 14 '06 #2
On Tue, 14 Mar 2006 09:18:55 +0800, "Mark Harris" <it******@nospam.nospam>
wrote:
Hi Brett,

I think what you are after is a Hashtable ;)

- Mark

Or qsort and binary search.
On Tue, 14 Mar 2006 08:41:53 +0800, Brett Romero <ac*****@cygen.com> wrote:
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
Mar 14 '06 #3
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

Mar 14 '06 #4
Brett Romero <ac*****@cygen.com> wrote:

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

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 14 '06 #5
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

Mar 14 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Alexander Tulai | last post by:
Hi, I've started using Java recently so the question may seem trivial. Is it possible to get rid of an object by simply creating a method (let's call it "die") in which one simply includes the...
4
by: Max Harvey | last post by:
Hi, I have looked at the example called "Open Parameter queries from code" from the site http://www.mvps.org/access/queries/qry0003.htm I made up a test which I though looked pretty close...
12
by: Sunny | last post by:
Hi All, I have a serious issue regarding classes scope and visibility. In my application, i have a class name "TextFile", and also a few other classes like "TotalWords", "TotalLines" and etc..,...
4
by: kscdavefl | last post by:
I ahve a datagrid on a web form. I need to change the value in column 3 as follows. If the value in column 3 reads 0, I want to change it to read YES. How can I accomplish this task. ...
0
by: Khuzema | last post by:
Dear All, I am using issue tracker architecture and developed business object for my application. Now, in VS Beta 2, I humbly want to know how i can have same feature as dataset, in my business...
2
by: Ian Gore | last post by:
Hi, I'm relatively new to VB.NET so I'd be grateful if someone could point out what I don't understand here... 1) Creating a strongly typed collection by inheriting CollectionBase. This is...
6
by: Scott M. Lyon | last post by:
As I mentioned in my other post, I'm attempting to, using COM Interop so I can update existing VB6 code to (for several specific functions) return a Hashtable from a .NET library. I've had...
0
by: dlakshmi | last post by:
Hi, i am creating windows services which is used existing vb6 com. I have converted my vb dll to interop dll using tlbimport. VB6 dll has collection class object. If am runing my code in...
5
by: Academia | last post by:
I use to do this: Dim index As Integer = CType(sender, MenuItem).Index but ToolStripMenuItems do not have an Index property so I can set the Tag or MergeIndex property of those items and use...
0
by: =?Utf-8?B?RmFicml6aW8gQ2lwcmlhbmk=?= | last post by:
I need to access classic ASP intrinsic objects and their properties from a ..net assembly wrapped to COM. The COM .net assembly is then instanciated from a classic ASP page with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.