dmytro
dmytro

Reputation: 346

how can i get an access to the elements of array in the class

I have a problem which I don't know how to solve. I have a class. This class has two arrays. I would like to get access via properties. How can I do it? I tried to use indexers, but it is possible if I have only one array. Here what I want to do:

public class pointCollection
{
    string[] myX; 
    double[] myY;
    int maxArray;
    int i;
    public pointCollection(int maxArray)
    {
        this.maxArray = maxArray;
        this.myX = new string[maxArray];
        this.myY = new double[maxArray];           
    }
    public string X //It is just simple variable
    {
        set { this.myX[i] = value; }
        get { return this.myX[i]; }            
    }
    public double Y //it's too
    {
        set { this.myY[i] = value; }
        get { return this.myY[i]; }            
    }
}

With this code, my X and Y are only simple variables, but not arrays. If I use indexers, I get access only to one array:

    public string this[int i]
    {
        set { this.myX[i] = value; }
        get { return this.myX[i]; }            
    }

But how can I get access to second array? Or I can't use property in this case? And I need only use:

    public string[] myX; 
    public double[] myY;

Upvotes: 1

Views: 981

Answers (3)

user467384
user467384

Reputation: 1167

An example with Tuples.

public class pointCollection
{
    Tuple<String,Double>[] myPoints;
    int maxArray;
    int i;
    public pointCollection(int maxArray)
    {
        this.maxArray = maxArray;
        this.myPoints = new Tuple<String,Double>[maxArray];
    }
    public Tuple<String,Double> this[int i]
    {
        set { this.myPoints[i] = value; }
        get { return this.myPoints[i]; }            
    }
}

And to access the points you do...

pointCollection pc = new pointCollection(10);
// add some data
String x = pc[4].Item1; // the first entry in a tuple is accessed via the Item1 property
Double y = pc[4].Item2; // the second entry in a tuple is accessed via the Item2 property

Upvotes: 1

Xcelled
Xcelled

Reputation: 2104

The closest you'll come without either changing your data structure or moving to methods is to make a property that returns each array, much like you did in your first code block, except without the [i].

Then, you do var x = instanceOfPointCollection.MyX[someI]; for example.

Upvotes: 0

dmay
dmay

Reputation: 1335

If I got it right, you need some kind or read/write-only wrapper for arrays to be exposed as properties.

public class ReadWriteOnlyArray<T>{

    private T[] _array;

    public ReadWriteOnlyArray(T[] array){
        this._array = array;
    }

    public T this[int i]{
        get { return _array[i]; }
        set { _array[i] = value; }
    }
}

public class pointCollection
{
    string[] myX; 
    double[] myY;
    int maxArray;

    public ReadWriteOnlyArray<string> X {get; private set;}
    public ReadWriteOnlyArray<double> Y {get; private set;}

    public pointCollection(int maxArray)
    {
        this.maxArray = maxArray;
        this.myX = new string[maxArray];
        this.myY = new double[maxArray];           
        X = new ReadWriteOnlyArray<string>(myX);
        Y = new ReadWriteOnlyArray<double>(myY);
    }
}

and usage

 var c = new pointCollection(100);
 c.X[10] = "hello world";
 c.Y[20] = c.Y[30] + c.Y[40];

Upvotes: 0

Related Questions