473,569 Members | 2,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

storing items in list/collection by an ID key

Hi,

I have a number of objects i would like to store in a list like struture.

They already have ID's associated with them so i would like to utilise these
ids as accessors the list..

At the moment I have approx 10 objects with ids that are spread out,

e.g 1, 12, 24, 56, 200,320....

now i dont want to create a list with space for 320 items when there are
only 10 actual objects which is what I would have to do if i used the above
IDs as the key to store/access the objects.

I'm sure this is a common problem and that there are solutions to avoid this
waste of space.

I'd appreciate if anyone can give me advice on how to strore these items
efficently and how to access them from a list like structure,

Thanks
Macca
Oct 2 '06 #1
5 1962
Have a look at the 'System.Collect ions' items. I guess in your example
especially the Hashtable would be interesting. Here's the example for
the .NET help of hashtables:

-------------------------------------------------------------------
using System;
using System.Collecti ons;
public class SamplesHashtabl e {

public static void Main() {

// Creates and initializes a new Hashtable.
Hashtable myHT = new Hashtable();
myHT.Add("First ", "Hello");
myHT.Add("Secon d", "World");
myHT.Add("Third ", "!");

// Displays the properties and values of the Hashtable.
Console.WriteLi ne( "myHT" );
Console.WriteLi ne( " Count: {0}", myHT.Count );
Console.WriteLi ne( " Keys and Values:" );
PrintKeysAndVal ues( myHT );
}
public static void PrintKeysAndVal ues( Hashtable myList ) {
IDictionaryEnum erator myEnumerator = myList.GetEnume rator();
Console.WriteLi ne( "\t-KEY-\t-VALUE-" );
while ( myEnumerator.Mo veNext() )
Console.WriteLi ne("\t{0}:\t{1} ", myEnumerator.Ke y,
myEnumerator.Va lue);
Console.WriteLi ne();
}
}
/*
This code produces the following output.

myHT
Count: 3
Keys and Values:
-KEY- -VALUE-
Third: !
Second: World
First: Hello
*/
-------------------------------------------------------------------

Oct 2 '06 #2

Jeroen wrote:
Have a look at the 'System.Collect ions' items. I guess in your example
especially the Hashtable would be interesting. Here's the example for
the .NET help of hashtables:

-------------------------------------------------------------------
using System;
using System.Collecti ons;
public class SamplesHashtabl e {

public static void Main() {

// Creates and initializes a new Hashtable.
Hashtable myHT = new Hashtable();
myHT.Add("First ", "Hello");
myHT.Add("Secon d", "World");
myHT.Add("Third ", "!");

// Displays the properties and values of the Hashtable.
Console.WriteLi ne( "myHT" );
Console.WriteLi ne( " Count: {0}", myHT.Count );
Console.WriteLi ne( " Keys and Values:" );
PrintKeysAndVal ues( myHT );
}
public static void PrintKeysAndVal ues( Hashtable myList ) {
IDictionaryEnum erator myEnumerator = myList.GetEnume rator();
Console.WriteLi ne( "\t-KEY-\t-VALUE-" );
while ( myEnumerator.Mo veNext() )
Console.WriteLi ne("\t{0}:\t{1} ", myEnumerator.Ke y,
myEnumerator.Va lue);
Console.WriteLi ne();
}
}
/*
This code produces the following output.

myHT
Count: 3
Keys and Values:
-KEY- -VALUE-
Third: !
Second: World
First: Hello
*/
-------------------------------------------------------------------
Out of curiosity, why do you use an IDictionaryEnum erator?
foreach(Diction aryEntry d in myList) also works. The enumerator method
works fine, it just wouldn't be my first choice, so I'm wondering if
I'm missing out on something :)

To the OP, if you know the ID, the following also works.
Console.WriteLi ne( myList["First"].ToString());
Where "First" is they key of one of the items from the example posted
above.

Oct 2 '06 #3

DeveloperX schreef:
Out of curiosity, why do you use an IDictionaryEnum erator?
foreach(Diction aryEntry d in myList) also works. The enumerator method
works fine, it just wouldn't be my first choice, so I'm wondering if
I'm missing out on something :)
I'm afraid that I can't answer that. The problem posted initially
seemed to ask for hashtables. I just posted an example from the .NET
help, so it's actually not my code, I wouldn't know the answer to your
question.

Maybe someone else does?

Oct 2 '06 #4
There's no obvious reason I see not to use foreach. Where did you get
that example code?

Jeroen wrote:
DeveloperX schreef:
Out of curiosity, why do you use an IDictionaryEnum erator?
foreach(Diction aryEntry d in myList) also works. The enumerator method
works fine, it just wouldn't be my first choice, so I'm wondering if
I'm missing out on something :)

I'm afraid that I can't answer that. The problem posted initially
seemed to ask for hashtables. I just posted an example from the .NET
help, so it's actually not my code, I wouldn't know the answer to your
question.

Maybe someone else does?
Oct 2 '06 #5
Macca,
>From context clues I'm assuming you'd like to have the objects ordered
by ID so that when you enumerate them the IDs are monotonically
increasing. Is that correct?

If that's the case then you'll probably want to use
System.Collecti ons.SortedList or if you're 2.0 then use either
System.Collecti ons.Generics.So rtedList or
System.Collecti ons.Generics.So rtedDictionary. The difference between
the later two is that SortedList is implemented as an array so it use
less memory at the expense of slower insertions and deletions while
SortedDictionar y is implemented as a red black tree so it has faster
insertions and deletions at the expensive of more memory.

If that's not the case and you really don't care about the order the
objects then use System.Collecti ons.Hashtable or
System.Collecti ons.Generics.Di ctionary.

Brian

Macca wrote:
Hi,

I have a number of objects i would like to store in a list like struture.

They already have ID's associated with them so i would like to utilise these
ids as accessors the list..

At the moment I have approx 10 objects with ids that are spread out,

e.g 1, 12, 24, 56, 200,320....

now i dont want to create a list with space for 320 items when there are
only 10 actual objects which is what I would have to do if i used the above
IDs as the key to store/access the objects.

I'm sure this is a common problem and that there are solutions to avoid this
waste of space.

I'd appreciate if anyone can give me advice on how to strore these items
efficently and how to access them from a list like structure,

Thanks
Macca
Oct 2 '06 #6

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

Similar topics

1
1141
by: Eric Lilja | last post by:
Hello, I am currently designing a small program that's supposed to become a helper program for a computer game. The program is to store a number of recipes and this collection of recipes is expected to grow to quite big over time. Each recipe can be categorized into one of several categories, for example: wood-working, tailoring, brewing,...
4
1259
by: Dharmesh | last post by:
Hi every1, I am learning ASP.NET and came accross problem of storing my classes object and I want its scope to be request. Is there a way do that. I found the only way to store structured values in with scope request was with ViewState, but for the ViewState to work the page has to post back to itself. But my page does not posts back. I...
3
7372
by: Dany P. Wu | last post by:
Hi everyone, One of my Windows forms contain two listbox controls, with Add and Remove buttons between them. The idea is to allow users to select multiple items from one ListBox, click the Add button, and the selected items will move to the second ListBox. I've been trying to use the ListBox.SelectedObjectCollection with no success. It...
9
7307
by: Don | last post by:
Is there any way to detect when an item has been added to the Items collection of a combobox or listbox? I am inheriting a Combobox and want to validate items before they are added to the combobox, but I can't find anything that will let me do that. - Don
2
2795
by: hsuntn | last post by:
I am grabbing Outlook MailItems using the Items property on my Outlook inbox. When I iterate through them, I notice that they are not ordered in ReceivedTime or CreationTime order. For example, Items.ReceivedTime is April 2005 Items.ReceivedTime is May 2005 Items.ReceivedTime is October 2005 Items.ReceivedTime is June 2005 I did some...
5
1704
by: Bryan | last post by:
I have a class 'TagType' with an ilist member 'Props' that holds a collection of another class called 'Prop'. I let the user create TagTypes and save multiple properties (Props) in them. I am having a problem storing a Prop object in a TagType object. The code below is giving me a "Object reference not set to an instance of an object."...
10
6754
by: pamelafluente | last post by:
Hi I have a sorted list with several thousands items. In my case, but this is not important, objects are stored only in Keys, Values are all Nothing. Several of the stored objects (might be a large number) have to be removed (say when ToBeRemoved = true). class SomeObj ToBeRemoved be a boolean field end class
0
1892
by: marcobx | last post by:
I have a ComboBox and a List of objects to popolate items. I use the DataSource property and not the List collection: List<foolst = new List<foo>; //add elements into the list //... comboBox1.DataSource = lst; This work fine, items appear. Now I have a button near the combo box and I want to add a new element
10
1479
by: mk | last post by:
Calvin Spealman wrote: Well, basically nothing except I need to remember I have to do that. Suppose one does that frequently in a program. It becomes tedious. I think I will define some helper function then: .... fundict = newfun ....
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6287
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5223
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.