Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Retreiving by index OR key

0 views
Skip to first unread message

Andreas Håkansson

unread,
Jul 6, 2002, 1:21:20 PM7/6/02
to
I'm bulding a class that acts like a collection class and I want to be able
to
retrieve the contents either by an index number or a key value. What would
be the best way to do this? Store the objects in an array and store the keys
with the array index in a hashtable ? Something like this

public object this[int index]
{
get { // get the item at position index in the array and return it }
}

public object this[string key]
{
get
{
// look up the key in the hashtable and grab the store
// index value. Then get the item at the retrieved index
// value in the array and return it
}
}

Would this be the way to go, or is there perhaps a more suitable
method of doing this?

--
Andreas Håkansson
Student of Software Engineering
andreas (at) selfinflicted.org

In a world in which we are all slaves to the laws of gravity,
I'm proud to be counted as one of the freedom fighters.

Kurt Lange

unread,
Jul 6, 2002, 1:35:28 PM7/6/02
to

Take a look at the SortedList Class

>.
>

Chad Myers

unread,
Jul 6, 2002, 1:37:57 PM7/6/02
to
That's how most of the .NET classes do it.

Although, usually the key is an 'object' class
rather than string, but your specific implementation
may only take strings as keys.

-c

"Andreas Håkansson" <and...@selfinflicted.org> wrote in message
news:uG9zlGRJCHA.2728@tkmsftngp10...

Andreas Håkansson

unread,
Jul 6, 2002, 2:30:12 PM7/6/02
to
SortedList wont work since the order of the stored objects will
change as they are reordred based on the supplied key values,
thus using GetByIndex() will not guarantee that the same object
is always returned for a given index number.

I guess I'll have to write my own linked list implementation and
an add the required iterator functionality to it.

--
Andreas Håkansson
Student of Software Engineering
andreas (at) selfinflicted.org

In a world in which we are all slaves to the laws of gravity,
I'm proud to be counted as one of the freedom fighters.

"Kurt Lange" <lange...@hotmail.com> wrote in message
news:16ba001c22513$852b02b0$35ef2ecf@TKMSFTNGXA11...

Andreas Håkansson

unread,
Jul 6, 2002, 2:32:52 PM7/6/02
to
No it's not. Although most of the .NET classes uses an indexer that
takes "object key" as a parameter the kay can't ba an index value, it
has to be a key value. So I can't use it to retrieve both by index and
by key. The closest thing is to use the SortedList class and use the
indexer to retrieve by key and the GetByIndex() method to retrive by
index, but it won't work since it's sortes based on the provided keys
which means the index can change as soon as something is added to
the sorted list.

--
Andreas Håkansson
Student of Software Engineering
andreas (at) selfinflicted.org

In a world in which we are all slaves to the laws of gravity,
I'm proud to be counted as one of the freedom fighters.

"Chad Myers" <cmy...@N0.SP.4M.austin.rr.com> wrote in message
news:VVFV8.45582$q53.1...@twister.austin.rr.com...

Joel Leong

unread,
Jul 9, 2002, 5:17:17 AM7/9/02
to
try hashtable if the object u wanna to store is a object type or
NameValueCollection if it is a string.

Joel Leong

"Andreas Håkansson" <and...@selfinflicted.org> wrote in message
news:uG9zlGRJCHA.2728@tkmsftngp10...

Andreas Håkansson

unread,
Jul 9, 2002, 6:52:57 AM7/9/02
to
A HashTable won't work since it doesn't store in a linear fashion, that is
no mather what kind of probing it uses it will be full of holes. So I can't
guarantee that I can get index 1,2,3,4 since it migth be 1,5,9,20 see
what I mean?

--
Andreas Håkansson
Student of Software Engineering
andreas (at) selfinflicted.org

In a world in which we are all slaves to the laws of gravity,
I'm proud to be counted as one of the freedom fighters.

"Joel Leong" <sc...@tm.net.my> wrote in message
news:#hf$BjyJCHA.1600@tkmsftngp13...

0 new messages