tags:

views:

128

answers:

4

I need a collection that

  • contains a set of objects linked to a double.
  • The sequence of these pairs should be arbitrary set by me (based on an int I get from the database) and be static throughout the lifecycle.
  • The number of entries will be small (0 ~ 20) but varying.
  • The collection should be itteratable.
  • I don't have to search the collection for anything.
  • The double will be changed after intialization of the collection.
  • I would like to work with existing datatypes (no new classes) since it will be used in my asp.net mvc controllers, views and services and I don't want them to all to have a dependency on a library just for this stupid holder class.

I thought

IDictionary<int, KeyvaluePair<TheType, double>>

would do the trick, but then I can't set the double after init.

--Edit--
I found out that the classes generated by the linq 2 sql visual studio thingy are actually partial classes so you can add to them whatever you want. I solved my question by adding a double field to the partial class.
Thanks all for the answers you came up with.

+2  A: 

It sounds like you may just want an equivalent of KeyValuePair, but mutable. Given that you're only using it as a pair of values rather than a key-value pair, you could just do:

public class MutablePair<TFirst, TSecond>
{
    public TFirst First { get; set; }
    public TSecond Second { get; set; }

    public MutablePair()
    {
    }

    public MutablePair<TFirst, TSecond>(TFirst first, TSecond second)
    {
        First = first;
        Second = second;
    }
}

This doesn't override GetHashCode or Equals, because you're not actually using those (as it's in a value position).

Jon Skeet
Your solution is correct, but my requirements weren't complete. Updated them. Could you have a look?
borisCallens
A: 

Well, KeyValuePair is immutable (which is a good thing), so you'll have to replace the entire value of KeyValuePair, not just the part of it:

yourDict[10] = new KeyValuePair<TheType, Double>(yourDict[10].Key, newValue);

... or think like Jon Skeet. Gah. :)

Lasse V. Karlsen
+1  A: 
struct MyPair
{
    public object TheType;
    public double Value;
}

MyPair[] MyColleccyion = new MyPair[20];
James Curran
I updated the requirements some.
borisCallens
A: 

How about this

public class ListThing<TKey, TValue> : Dictionary<TKey, TValue>
{
    public double DoubleThing { get; set; }

    public ListThing(double value)
    {
        DoubleThing = value;
    }
}
Hath