PC Review


Reply
Thread Tools Rate Thread

Dictionary with keys only. Anything ?

 
 
pamela fluente
Guest
Posts: n/a
 
      13th Feb 2007

Sometimes I define a
System.Collections.Generic.Dictionary(Of Key, ...)

but actually I only use the Key part. Is there any collection which I
can use
which is similar to a Dictionary, but without the value part ?
I mean just a list of unique key Object , with a O(1) retrieval
operation ?

-Pam

 
Reply With Quote
 
 
 
 
Michael D. Ober
Guest
Posts: n/a
 
      13th Feb 2007
All collections have value parts. If your keys are strings, use
System.Collections.Specialized.StringCollection. Otherwise, try

dim Valueless as new Dictionary(of KeyType, KeyType)
Valueless.add(key, key)

At least this way you don't have two objects floating around memory - just
one with two references.

Mike Ober.

"pamela fluente" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> Sometimes I define a
> System.Collections.Generic.Dictionary(Of Key, ...)
>
> but actually I only use the Key part. Is there any collection which I
> can use
> which is similar to a Dictionary, but without the value part ?
> I mean just a list of unique key Object , with a O(1) retrieval
> operation ?
>
> -Pam
>
>




 
Reply With Quote
 
Branco Medeiros
Guest
Posts: n/a
 
      13th Feb 2007
On Feb 13, 12:03 am, "pamela fluente" <pamelaflue...@libero.it> wrote:
> Sometimes I define a
> System.Collections.Generic.Dictionary(Of Key, ...)
>
> but actually I only use the Key part. Is there any collection which I
> can use
> which is similar to a Dictionary, but without the value part ?
> I mean just a list of unique key Object , with a O(1) retrieval
> operation ?
>
> -Pam


I'm particularly fond of
System.Collections.ObjectModel.KeyedCollection(Of Key, Item). It's
usefull when the item is also the key, or the key is a property of the
item itself. Unfortunately its an abstract class, which means you must
inherit it, because there's a GetKeyForItem method that needs
implementation. Indexed access is O(1), while keyed access is near
O(1) -- or so the docs say. Besides, it has the definite advantage (to
me) that it keeps the insertion order (which a Dictionary won't).

HTH.

Regards,

Branco.




 
Reply With Quote
 
pamela fluente
Guest
Posts: n/a
 
      13th Feb 2007
On 13 Feb, 04:50, "Michael D. Ober" <obermd.@.alum.mit.edu.nospam>
wrote:
> All collections have value parts. If your keys are strings, use
> System.Collections.Specialized.StringCollection.


The times I need keys only are often those where the object is also
used as a key (through an appropriate comparer).

Otherwise, try
>
> dim Valueless as new Dictionary(of KeyType, KeyType)
> Valueless.add(key, key)


Yes that's what I am doing. Or else I do Valueless.add(key, Nothing)
(is this better?) which anyway seems quite awkward to me.

>
> At least this way you don't have two objects floating around memory - just
> one with two references.
>
> Mike Ober.
>


 
Reply With Quote
 
pamela fluente
Guest
Posts: n/a
 
      13th Feb 2007
On 13 Feb, 06:45, "Branco Medeiros" <branco.medei...@gmail.com> wrote:
> On Feb 13, 12:03 am, "pamela fluente" <pamelaflue...@libero.it> wrote:
>
> > Sometimes I define a
> > System.Collections.Generic.Dictionary(Of Key, ...)

>
> > but actually I only use the Key part. Is there any collection which I
> > can use
> > which is similar to a Dictionary, but without the value part ?
> > I mean just a list of unique key Object , with a O(1) retrieval
> > operation ?

>
> > -Pam

>
> I'm particularly fond of
> System.Collections.ObjectModel.KeyedCollection(Of Key, Item). It's
> usefull when the item is also the key, or the key is a property of the
> item itself. Unfortunately its an abstract class, which means you must
> inherit it, because there's a GetKeyForItem method that needs
> implementation. Indexed access is O(1), while keyed access is near
> O(1) -- or so the docs say. Besides, it has the definite advantage (to
> me) that it keeps the insertion order (which a Dictionary won't).


Hmm I might give it a try. Isn't strange that there isn't a collection
of keys only ? I think it could be nice to have it

>
> HTH.
>
> Regards,
>
> Branco.



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A dictionary with many keys for the same value Martin Microsoft C# .NET 6 13th Oct 2006 03:51 AM
Problem with Dictionary<T,U>.Keys enumerator returning invalid keys Brian Richards Microsoft Dot NET Framework 2 10th Jun 2006 03:09 AM
Problem with Dictionary<T,U>.Keys enumerator returning invalid keys Brian Richards Microsoft C# .NET 5 5th Jun 2006 11:56 PM
Problem with Dictionary<T,U>.Keys enumerator returning invalid keys Brian Richards Microsoft Dot NET 1 5th Jun 2006 11:54 PM
dictionary keys =?Utf-8?B?anVsaW8=?= Microsoft Excel Programming 11 21st Jun 2004 08:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:08 AM.