-4

Is it possible to have two or more indexer (or something simillar) with the same signature?

So it would look something like this

    private int[,] X;
    public int this[int a]
    {
        set
        {
            X[a, 0] = value;
        }

    }
    public int this[int a]
    {
        set
        {
            X[a, 1] = value;
        }
    }

This gives an error because both methods are nameless and with the same signature. So is it possible to name these methods but keep the abillity to set a value. i.e.

C.FirstCol(2) = 3; //Same as C.X[2,0] = 3
C.SecondCol(5) = 4; //Same as C.X[5,1] = 4

EDIT: I'll try to explain it better:

The line C.SetX(value) can be changed to C.X = value by using properties.

The line C.Set(num, value) can be changed to C[num] = value by using indexer

What I am asking is how can I change these lines

C.SetA(num, value)
C.SetB(num, value)

To

C.A[num] = value
C.B[num] = value

Or

C.A(num) = value
C.B(num) = value

Without having A or B as arrays

RexMan85
  • 59
  • 1
  • 1
  • 6

4 Answers4

1

You could use a two-dimensional indexer (if I understand your problem correctly!):

private int[,] X;
public int this[int row, int column]
{
    set
    {
        X[row, column] = value;
    }

}

Used like:

C[2, 0] = 3;
C[5, 1] = 4;
TVOHM
  • 2,740
  • 1
  • 19
  • 29
0

It's not possible - how would the compiler know which indexer you want to use? And there is no syntax as you would like to have in your sample.

You can introduce the method like this:

public void FirstCol(int rowIndex, int value)
{
    X[rowIndex, 0] = value;
}

and similar one for second column, etc. But to be honest I can't see the point. Maybe it would be better to introduce 2-dimensional indexer instead?

tdragon
  • 3,209
  • 1
  • 16
  • 17
  • To know which indexer to use - that is why I asked if it's possible to use a method (with a name) as a `set`, like `C.FirstRow(2) = 3;`. And I know that in this example it would be better to use a two argument indexer, but it was just an example to show what I am looking for – RexMan85 Mar 14 '16 at 17:40
0

You can create an index wrapper:

class Indexer
{
  private int[,] data;
  private int index;

  public Indexer(int index, int[,] data)
  {
    this.index = index;
    this.data = data;
  }

  public int this[int a]
  {
    set {data[a,index] = value;}
  }
}

And then use it in your code:

class Foo
{
  private int[,] data;
  private readonly Indexer first;
  private readonly Indexer second;

  public Foo()
  {
    first = new Indexer(0, data);
    second = new Indexer(1, data);
  }

  public Indexer First
  {
    get{ return first;}
  }

  public Indexer Second
  {
    get{ return second;}
  }
}

Now you can say:

Foo f = new Foo();
f.First[2] = 3;
f.Second[5] = 4;
Sean
  • 60,939
  • 11
  • 97
  • 136
0

This answer is meant to be demonstrative, but not necessarily practical. If you define a class like this:

class Indexer<TKey, TValue>
{
    Func<TKey, TValue> getter;
    Action<TKey, TValue> setter;
    public Indexer(Func<TKey, TValue> getter, Action<TKey, TValue> setter)
    {
        this.getter = getter;
        this.setter = setter;
    }
    public TValue this[TKey key]
    {
        get { return getter(key); }
        set { setter(key, value); }
    }
}

You could use it like this:

Indexer<int, int> firstColumn = new Indexer<int, int>(
    i => X[i, 0],
    (i, v) => X[i, 0] = v);
public Indexer<int, int> FirstColumn
{
    get { return firstColumn; }
}

Indexer<int, int> secondColumn = new Indexer<int, int>(
    i => X[i, 1],
    (i, v) => X[i, 1] = v);
public Indexer<int, int> SecondColumn
{
    get { return secondColumn; }
}

Which would then let you do this:

C.FirstColumn[2] = 3;
C.SecondColumn[4] = 5;

However, I would not advise doing this! It would be much more straightforward to simply have a method Set(int i, int j, int value) or an indexer int this[int i, int j].

Timothy Shields
  • 75,459
  • 18
  • 120
  • 173