Reducing repetition of sequence-like properties I have a class that is a wrapper for memory positions of a hardware device.

I need to data-bind each of this positions, so I created a class with hardcoded properties, one for each position, so that I can bind a WPF DataGrid to it.

Problem is: given the sequential nature of the positions, the code became quite repetitive.

Model(.cs):



public class TabelaMemoria
{

    public string Nome { get; private set; }

    int Numero { get; set; }
    MiotoolWrapper _device;

    public TabelaMemoria(MiotoolWrapper deviceWrapper, string nome, int numero)
    {
        _device = deviceWrapper;
        Nome = nome;
        Numero = numero;
    }

    public float A1 
    {
        get { return getVal(0); } 
    }

    public float B1
    { 
        get { return getVal(1); } 
    }

    public float A2 
    {
        get { return getVal(2); } 
    }

    public float B2
    {
        get { return getVal(3); } 
    }

    public float A3
    {
        get { return getVal(4); } 
    }

    public float B3 
    { 
        get { return getVal(5); }
    }

    public float A4 
    { 
        get { return getVal(6); }
    }

    public float B4
    { 
        get { return getVal(7); }
    }



    private float getVal(int p)
    {
        return _device.GetCalibrationValue(Numero, p);
    }

}


View(.xaml):



&lt;DataGrid
    ItemsSource="{Binding Memorias}"  &lt;!-- of type IEnumerable&lt;TabelaMemoria&gt; --&gt;
    AutoGenerateColumns="False"
    ColumnWidth="Auto"&gt;

    &lt;DataGrid.RowHeaderStyle&gt;
        &lt;Style TargetType="DataGridRowHeader"&gt;
            &lt;Setter Property="Content" Value="{Binding Nome}"/&gt;
        &lt;/Style&gt;
    &lt;/DataGrid.RowHeaderStyle&gt;
    &lt;DataGrid.Columns&gt;
        &lt;DataGridTextColumn Header="A1" Binding="{Binding A1}" Width="100"/&gt;
        &lt;DataGridTextColumn Header="B1" Binding="{Binding B1}" Width="100"/&gt;
        &lt;DataGridTextColumn Header="A2" Binding="{Binding A2}" Width="100"/&gt;
        &lt;DataGridTextColumn Header="B2" Binding="{Binding B2}" Width="100"/&gt;
        &lt;DataGridTextColumn Header="A3" Binding="{Binding A3}" Width="100"/&gt;
        &lt;DataGridTextColumn Header="B3" Binding="{Binding B3}" Width="100"/&gt;
        &lt;DataGridTextColumn Header="A4" Binding="{Binding A4}" Width="100"/&gt;
        &lt;DataGridTextColumn Header="B4" Binding="{Binding B4}" Width="100"/&gt;
    &lt;/DataGrid.Columns&gt;
&lt;/DataGrid&gt;
I don't have VS on my laptop, but perhaps an enum might make it a bit simpler?

(Heads up: You might need to make a few changes to get this to work)

Model

public class TabelaMemoria {
    public string Nome { get; private set; }

    int Numero { get; set; }
    MiotoolWrapper _device;

    public TabelaMemoria(MiotoolWrapper deviceWrapper, string nome, int numero)
    {
        _device = deviceWrapper;
        Nome = nome;
        Numero = numero;
    }

    public enum Position{
        A1=0,
        B1,
        A2,
        B2,
        A3,
        B3,
        A4,
        B4
    }

    public float getval(Position p){
        return _device.GetCalibrationValue((int)p);
    }
}


View.xaml

&lt;DataGrid.Columns x:Name="grdColumns"/&gt;


View.Xaml.cs

public View{
    public View(){
        InitializeComponent();
        InitializePositions();
    }

    private void InitializePositions(){
        foreach (Position p in Enum.GetValues(typeof(Position))) {
            DataGridTextColumn dgrid = new DataGridTextColumn();
            dgrid.Width = 100;
            dgrid.Header = p.ToString();
            dgrid.Text = (int)p;
        }
    }
}


Now if you add anymore positions, just add it to the enum and it should automatically be picked up. I know it screws up the binding, but I wasn't sure what your specific use case was.

If nothing else, perhaps this will get your gears spinning on a better solution.
Good luck, mate!