473,408 Members | 1,955 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,408 software developers and data experts.

Collection with Key and Index

I am trying to create a base class that contains a collection that MUST be
able to be referenced by both a string key and a positional index. This
collection MUST keep the items in the same order as they are added.

In 2.0, is there a collection that would support this?

The List<> works to keep the order but doesn't allow me to reference the
items by key like the Dictionary<> collection does, but I don't think the
Dictionary collection is guarenteed to keep the items in order.

Any ideas?

Gregory McCallum, MSCD
gm*******@honovi.com
Jun 27 '06 #1
5 18459
Gregory,

Try the SortedList<TKey, TValue> class.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"gmccallum" <gm*******@discussions.microsoft.com> wrote in message
news:86**********************************@microsof t.com...
I am trying to create a base class that contains a collection that MUST be
able to be referenced by both a string key and a positional index. This
collection MUST keep the items in the same order as they are added.

In 2.0, is there a collection that would support this?

The List<> works to keep the order but doesn't allow me to reference the
items by key like the Dictionary<> collection does, but I don't think the
Dictionary collection is guarenteed to keep the items in order.

Any ideas?

Gregory McCallum, MSCD
gm*******@honovi.com

Jun 27 '06 #2
Thanks for the help, but that collection is used to keep a list sorted by the
string key not the position added like the List<> does.

I need a collection that acts like an ArrayList or List<> but allows me to
look up a value by a Key value also.

I have done this before in 1.1 but had to create a bit custom collection
with an overloaded indexer. I was hoping that 2.0 would just include this.
Something similiar to vb6 collection which allowed position indexes and also
keys.

Gregory McCallum, MSCD
gm*******@honovi.com

"Nicholas Paldino [.NET/C# MVP]" wrote:
Gregory,

Try the SortedList<TKey, TValue> class.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"gmccallum" <gm*******@discussions.microsoft.com> wrote in message
news:86**********************************@microsof t.com...
I am trying to create a base class that contains a collection that MUST be
able to be referenced by both a string key and a positional index. This
collection MUST keep the items in the same order as they are added.

In 2.0, is there a collection that would support this?

The List<> works to keep the order but doesn't allow me to reference the
items by key like the Dictionary<> collection does, but I don't think the
Dictionary collection is guarenteed to keep the items in order.

Any ideas?

Gregory McCallum, MSCD
gm*******@honovi.com


Jun 27 '06 #3
gmccallum <gm*******@discussions.microsoft.com> writes:
I am trying to create a base class that contains a collection that MUST be
able to be referenced by both a string key and a positional index. This
collection MUST keep the items in the same order as they are added.

In 2.0, is there a collection that would support this?

The List<> works to keep the order but doesn't allow me to reference the
items by key like the Dictionary<> collection does, but I don't think the
Dictionary collection is guarenteed to keep the items in order.


Use HashedLinkedList<String> or HashedArrayList<String> from the C5
generic collection library at http://www.itu.dk/research/c5/

Peter
Jun 27 '06 #4
Thanks, but that is too much overhead for what I am trying to do.
I went ahead and just wrote a custom collection that used both the IList<>
and IDictionary<> interfaces:

public class SwitchCollection<K, T> : IList<T>, IDictionary<string, T>
where K : IComparable
where T : SwitchInterface
{
// stored in two collections
List<T> _internalList = new List<T> ();
Dictionary<string, T> _internalDictionary = new Dictionary<string, T> ();

..
..
..

public T this[ int i ]
{
get
{
return _internalList[ i ];
}
set
{
if ( _internalDictionary.ContainsKey( value.Name ) == false )
throw new IndexOutOfRangeException ();
// Set the list
_internalList[ i ] = value;
// set the dictionary
_internalDictionary[ value.Name ] = value;
}
}

public T this[ string key ]
{
get
{
return _internalDictionary[ key ];
}
set
{

// Find in list
bool found = false;
int i = this.IndexOf ( value );
// Set the list
_internalList[ i ] = value;

// set the dictionary
_internalDictionary[ value.Name ] = value;
}
}
..
..
..
}


"Peter Sestoft" wrote:
gmccallum <gm*******@discussions.microsoft.com> writes:
I am trying to create a base class that contains a collection that MUST be
able to be referenced by both a string key and a positional index. This
collection MUST keep the items in the same order as they are added.

In 2.0, is there a collection that would support this?

The List<> works to keep the order but doesn't allow me to reference the
items by key like the Dictionary<> collection does, but I don't think the
Dictionary collection is guarenteed to keep the items in order.


Use HashedLinkedList<String> or HashedArrayList<String> from the C5
generic collection library at http://www.itu.dk/research/c5/

Peter

Jun 27 '06 #5
SP

"gmccallum" <gm*******@discussions.microsoft.com> wrote in message
news:86**********************************@microsof t.com...
I am trying to create a base class that contains a collection that MUST be
able to be referenced by both a string key and a positional index. This
collection MUST keep the items in the same order as they are added.

In 2.0, is there a collection that would support this?

The List<> works to keep the order but doesn't allow me to reference the
items by key like the Dictionary<> collection does, but I don't think the
Dictionary collection is guarenteed to keep the items in order.


KeyedCollection<TKey, TItem>

SP
Jun 28 '06 #6

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

Similar topics

2
by: SammyBar | last post by:
Hi, I'm trying to bind a custom collection class to a data grid, following the guidelines from the article http://msdn.microsoft.com/msdnmag/issues/05/08/CollectionsandDataBinding/default.aspx....
18
by: Scott | last post by:
I have a collection where the items in the collection are dates. I want to iterate over the collection and build a value list string for the rowsource of a listbox. The dates in the collection are...
2
by: S. Justin Gengo | last post by:
Hi, I've created a component that allows me to store database information for various types of databases my company uses. It uses a collection for each type of database. Everything is working...
4
by: Michael K. Walter | last post by:
I'd like to create a strongly-typed collection of objects that are indexed by a string value (key is a string). I'd also like to allow users to iterate over the collection using a For-each loop...
4
by: Michael | last post by:
Dear all .. If I want to use develop a user control and declare a public property which the type is System.Windows.Forms.GridTableStylesCollection For example : Public Class LookAndView...
1
by: Ruben | last post by:
I have a collection (PersonCollection inherits collectionbase and implements ITypedList it's a collection of Person objects) this collection also implements the IComponent interface, so I can drag...
14
by: Rich | last post by:
Yes, I need to store some values in an array type collection object that can hold 3 or more parameters per index. I have looked at the collection object, hashtable object and would prefer not to...
6
by: Arthur Dent | last post by:
How do you sort a generic collection derived from System.Collections.ObjectModel.Collection? Thanks in advance, - Arthur Dent
6
by: Erick | last post by:
I've created a class called Procs and a collection class called Processes which uses a hastable object to store the Procs. Now i want to enumerate with the "For each" to extract all the Procs in...
7
by: Dale | last post by:
I have a design question. I am creating a custom collection of products. The unique key for the products is productId which is an integer. By default, IndexOf(object obj), when obj is an int,...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
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...

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.