Tuesday, April 5, 2011

Is there a predefined class for 3 linked values like the Hashtable?

Is there a built in object that handles 3 linked values in a similar fashion to the hashtable? i.e. Key, Value1, Value2?

From stackoverflow
  • You could easily make one using a generic dictionary. Something like Dictionary<Key, KeyValuePair<Key, Value>>, or even Dictionary<Key, object[]>

    BenAlabaster : I thought about that - this library wasn't supposed to be heavy duty though so I was hoping not to have to build/implement anything extensive.
    Mike_G : Im not quite sure I understand. Dictionary and KeyValuePair are already implemented in .NET. You wouldnt have to build anything.
    BenAlabaster : Oops - I guess your whole text didn't post before I was commenting... yes, I see what you're getting at.
    Antony Perkov : I agree with Mike_G that this is probably the quickest way to implement what you want; but it probably won't yield the most maintainable code. In my experience, having a custom class as your "Value" with well named properties will be a lot clearer to people looking at your code for the first time.
    Mark Brackett : Don't like the object[] route, but +1 for KeyValuePair. There's a non generic Pair class somewhere in the BCL that I also use occasionally, but all I recall is that it's somewhere in one of the Control namespaces (WebControl, Control, ComponentModel or something...)
    Mike_G : Ya, object[] was just continue the example that the value in the dictionary could really be anything that can hold 2 values
  • I would have said generic dictionary too.

    If you didn't want to do anything extensive, just make a struct or some sort of tuple out of Value1, Value2 and make those the value of your dictionary's Key. Something like:

    Dictionary<Key, ThatTinyStructYouHadToCreate>
    

    Bad idea: If you didn't like that option, as far as "built-in" goes, a DataRow in a DataTable would give you that ability. While that's a very simple way to set it up, it'd also be a remarkably inefficient (as far as execution cost) way to go about it.

    bendewey : I was going to suggest this, nice.

0 comments:

Post a Comment