By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,934 Members | 1,318 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,934 IT Pros & Developers. It's quick & easy.

Collection with Key and Index

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.