0

I have a table in my entity model called prices. It has several fields named value0, value1, value2, value3, value4... (these are their literal names, sigh..). I cannot rename them or in any way change them.

What I would like is to use an extended entity to create a new property called values. This would be a collection containing value1, value2 etc...

To get access to the values I would then simply need to write prices.values[1]

I need property changed notification for this.

So far I have tried this;

    public partial class Prices
{

    private ObservableCollection<double?> values = null;


    public ObservableCollection<double?> Values
    {
        get
        {

            if (values != null)
                values.CollectionChanged -= values_CollectionChanged;
            else
                values = new ObservableCollection<double?>(new double?[14]);

            values[0] = value0;
            values[1] = value1;
            values[2] = value2;


            values.CollectionChanged += values_CollectionChanged;

            return values;
        }
        private set
        {
            value0 = value[0];
            value1 = value[1];
            value2 = value[2];

        }
    }

    private void values_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        Values = values;
    }
}

The issue comes when trying to set values. if I try to set a value by writing

prices.values[0] = someValue;

The new value is not always reflected in the collection (i.e. when I have previously set value and then try to overwrite the value).

I am willing to try any approach that would achieve my goal, I am not precious about having my solution fixed (although if anyone can explain what I'm missing that would be great!)

mark_h
  • 5,233
  • 4
  • 36
  • 52
  • 1
    You need to use another implementation of a collection because ObservableCollection does not let you override the indexer (and you need to override it to raise the event you need during set). By the way, why you need a collection if the elements are always 14? You could implement the indexer on Prices without using a collection... – bubi Apr 04 '16 at 17:34
  • Thanks bubi, what do you mean by implement the indexer on Prices without using a collection? (I'm fairly new to the industry, sorry) - You are right, there are always 14. – mark_h Apr 04 '16 at 18:31

1 Answers1

1

You could implement an indexer on Prices class without using a collection. You can use switch to select the property to write or you can use reflection. In this case I use reflection.

public double? this[int index]
{
    get
    {
        if (index < 0 || index > 13) throw new ArgumentOutOfRangeException("index");
        string propertyName = "Value" + index;
        return (double?)GetType().GetProperty(propertyName).GetValue(this);
    }
    set
    {
        if (index < 0 || index > 13) throw new ArgumentOutOfRangeException("index");
        string propertyName = "Value" + index;
        GetType().GetProperty(propertyName).SetValue(this, value);
        // Raise your event here
    }
}
bubi
  • 6,414
  • 3
  • 28
  • 45