Collection with Key and Index

G

Guest

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
(e-mail address removed)
 
N

Nicholas Paldino [.NET/C# MVP]

Gregory,

Try the SortedList<TKey, TValue> class.

Hope this helps.
 
G

Guest

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
(e-mail address removed)

Nicholas Paldino said:
Gregory,

Try the SortedList<TKey, TValue> class.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

gmccallum said:
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
(e-mail address removed)
 
P

Peter Sestoft

gmccallum said:
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
 
G

Guest

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;
}
}
..
..
..
}
 
S

SP

gmccallum said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top