Accessor of array elements on error c #

I have two arrays in my class and am trying to access them as follows. The first one works for theta, but the second one failed to compile for delta_theta. What's the correct way to make the second array without using this?

The error says: Error 1 Invalid array identifier: To declare a managed array, the rank specifier precedes the variable identifier. To declare a fixed-size buffer field, use the fixed keyword before the field type.

protected double[] theta = null;
protected double[] delta_theta = null;
public double this[int index] {
        get { return theta[index]; }
        set { theta[index] = value; }
}
public double Delta_Theta[int index]
{
        get { return delta_theta[index]; }
        set { delta_theta[index] = value; }
}

      

+3


source to share


3 answers


You cannot specify the indexer name in plain C # code, nor can you create multiple indexers with the same parameter types and distinguish them by name.

You can provide a name for other languages ​​(which support indexes) to use through DefaultMemberAttribute

, but you cannot use the name itself. (Except for COM components, and then only in C # 4 and up.)

Instead, you will need to expose a property of some type, which itself provides the corresponding index. Now, if you want to get and set, you can simply represent the array as a read-only property, perhaps like IList<double>

:

// Names changed to be more conventional
public IList<double> DeltaTheta
{
    get { return deltaTheta; }
}

      

Then clients can use:



foo.DeltaTheta[10] = 5.5;

      

eg.

For the read-only version, you can open ReadOnlyCollection<double>

:

public ReadOnlyCollection<double> DeltaTheta
{
    get { return new ReadOnlyCollection<double>(deltaTheta); }
}

      

or to avoid doing this every time, you could have a field for the wrapper: initialize it in the constructor and then return the same reference to the wrapper every time.

+3


source


There is no way to do this in this same class - the indexer must be in the form this(arguments)

, so you cannot have 2 that use the same type of index.

What you can do is create an inner class for the sole purpose of redirecting the indexing to your array and Delta_Theta

returning that class.



public IndexRedirector Delta_Theta 
{ 
  get { return new IndexRedirector { RedirectedArray:delta_theta }; 
}

class IndexRedirector
{ 
  public double[] RedirectedArray;
  public double this[int index] {
    get { return RedirectedArray[index]; }
    set { RedirectedArray[index] = value; }
  }
}

      

+1


source


You can create a wrapper indexer:

protected double[] theta = null;
protected double[] delta_theta = null;

public double this[int index] {
        get { return theta[index]; }
        set { theta[index] = value; }
}

public Indexer DeltaTheta { 
      // can be optimized according to delta_theta lifecycle
      get {return new Indexer(delta_theta);} 
}

// internal indexere wrapper
public class Indexer{
      double [] _data;
      public DoubleIndexer(double[] data ){   
          _data = data;
      }

      public double this[int index] {
            get { return _data[index]; }
            set { _data[index] = value; }
      }
}

      

and use like this:

obj[2]               // -> theta[2]
obj.DeltaTheta[5]    // -> delta_theta[5]

      

+1


source







All Articles