tags:

views:

934

answers:

9

I want to store values as key,value,value pair. My data is of type

Key -> int & both values -> ulong,

How to initialize & fetch values of such dictionary. I am using VS-2005.

If i use a class or struct then how do i fetch the values.

A: 

maybe you have to define a class say class Pair to hold your two value, and use int as the key.

Benny
+11  A: 

You can declare a class that stores both values and then use an ordinary dictionary. For example:

class Values {
    ulong Value1 {get;set;}
    ulong Value2 {get;set;}
}

var theDictionary=new Dictionary<int, Values>;

theDictionary.Add(1, new Values {Value1=2, Value2=3});
Konamiman
You could make Values a struct instead.
Yann Schwartz
Would probably be better to use KeyValuePair instead of a custom class, or at least use a struct.
Sune Rievers
A struct could give some space saving and would be more idiomatically correct, but the difference is minor and not so important.
Daniel Goldberg
Two ulongs are 16 bytes, so yep, this is one of the rare cases where a struct is appropriate. As for the KeyValuePair, I would'nt use it unless *actually* the two values are intended to be a key and a value, to make code more understandable.
Konamiman
+3  A: 

This would be an option:

Dictionary<int, KeyValuePair<ulong, ulong>> dictionary = new Dictionary<int, KeyValuePair<ulong, ulong>>();

If you want to add in a value: Key=1, Pair = {2,3}

dictionary.Add(1, new KeyValuePair<ulong, ulong>(2, 3));

If you want to retrieve those values:

var valuePair = dictionary[1];
ulong value1 = valuePair.Key;
ulong value2 = valuePair.Value;

Or simply:

ulong value1 = dictionary[1].Key;
naspinski
I prefer Konamiman's approach as it's cleaner, but this will do without a new class.
naspinski
but how do i initialize values to it..give some fetching code also :)
Royson
That would give two keys and a value. While it might work it would be confusing.
GraemeF
naspinski, that would seem like reinventing the wheel, dont you think?
astander
Semantically, a `KeyValuePair` consists of a *key* and a *value*. The OP wants to store *two values*, so although a `KeyValuePair` will do the trick it isn't a great "semantic" fit.
LukeH
not really reinventing a wheel, just a different approach - just sticking to base .Net types - Like I said, I would personally go with a new Class, but the OP asked for a Dictionary
naspinski
@astander: If you create a custom class, you can assign perhaps more meaningful names to the member variables than 'key' and 'value'.
Thanatos
Luke, I don't follow your logic - no matter what you return (if you are using a Dictionary like the OP asked for), since it has to have 2 values it will be an object of some type, be it a new class, array, list, kvp, etc.
naspinski
@naspinsky: If you use a `KeyValuePair` you're effectively telling the consumers/maintainers of your code that each pair consists of a *key* and a *value*, which is misleading (although perfectly functional).
LukeH
+1  A: 

I'm not sure I understand your question correctly, but if you want to store more than one value in the value part of Dictionary, you could do something like this:

var dic = new Dictionary<int,KeyValuePair<ulong,ulong>>();

You can use insert into the dictionary like this:

dic.Add(42, new KeyValuePair<ulong, ulong>(42, 42));
dic.Add(43, new KeyValuePair<ulong, ulong>(43, 43));

And fetch the values like so:

foreach (var a in dic)
{
    Console.WriteLine("Key: {0}, Value1: {1}, Value2: {2}",
        a.Key, a.Value.Key, a.Value.Value);
}
Sune Rievers
Semantically, a `KeyValuePair` consists of a *key* and a *value*. The OP wants to store *two values*, so although a `KeyValuePair` will do the trick it isn't a great "semantic" fit.
LukeH
Right, but I would still prefer this solution to writing a custom struct for this specific task.
Sune Rievers
A: 

You can make use of the KeyValuePair

Dictionary<int, KeyValuePair<ulong,ulong>> vals = new Dictionary<int, KeyValuePair<ulong, ulong>>();
astander
+3  A: 

Create a structure to store your values:

struct ValuePair
{
    public ulong Value1;
    public ulong Value2;
}

Dictionary initialization:

Dictionary<int, ValuePair> dictionary = new Dictionary<int, ValuePair>();

Maybe List is enogh, if you use int as key?

List:

List<ValuePair> list = new List<ValuePair>();
bniwredyc
A: 

Look at Wintellect.PowerCollections Namespace they have special structure Pair<(Of ) and collections to work with it or you'll need to code your own Pair type.

G2
+2  A: 

Create a Tuple class, in the System namespace:

public class Tuple<T1,T2>
{
    private readonly T1 _item1;
    private readonly T2 _item2;

    public Tuple(T1 item1, T2 item2)
    {
        this._item1 = item1;
        this._item2 = item2;
    }

    public T1 Item1 { get { return _item1; } }

    public T2 Item2 { get { return _item2; } }
}

And a static Tuple class with a Create method so you get type inference which is not available on constructors:

public static class Tuple
{
    public static Tuple<T1, T2> Create<T1, T2>(T1 item1, T2 item2)
    {
        return new Tuple<T1, T2>(item1, item2);
    }
}

Then, when you get onto .NET 4.0, you can delete these classes because they're in the Base Class Library (and are compatible with F# tuples!).

Coder 42
A: 

In C# 4, you'll have the Tuple type for your value, value pair.

There's an MSDN article describing the type and the design decisions behind it.

Martin R-L