Junior Programmer
Junior Programmer

Reputation: 379

Accessing and Setting Array-Type Properties

I know that a property has the following form:

class MyClass
{
    public int myProperty { get; set; }
}

This allows me to do this:

MyClass myClass = new MyClass();
myClass.myProperty = 5;
Console.WriteLine(myClass.myProperty); // 5

However, what can I do so that the following class:

class MyOtherClass
{
    public int[,] myProperty
    {
        get
        {
            // Code here.
        }
        set
        {
            // Code here.
        }
    }
}

Behaves as follows:

/* Assume that myProperty has been initialized to the following matrix:

myProperty = 1 2 3
             4 5 6
             7 8 9

and that the access order is [row, column]. */

myOtherClass.myProperty[1, 2] = 0;

/* myProperty = 1 2 3
                4 5 0
                7 8 9 */

Console.WriteLine(myOtherClass.myProperty[2, 0]); // 7

Thanks in advance!

Upvotes: 1

Views: 122

Answers (5)

dash
dash

Reputation: 91550

In addition to the other answers which expose the array directly, you could consider using an Indexer:

public class MyIndexedProperty
{
    private int[,] Data { get; set; }

    public MyIndexedProperty()
    {
        Data = new int[10, 10];
    }

    public int this[int x, int y] {

        get
        {
            return Data[x, y];
        }
        set
        {
            Data[x, y] = value;
        }
    }

}

So your class may look something like this:

public class IndexerClass
{

    public MyIndexedProperty IndexProperty { get; set; }

    public IndexerClass()
    {
        IndexProperty = new MyIndexedProperty();
        IndexProperty[3, 4] = 12;
    }

}

Note you'll need to ensure Data is initialized before accessing it - I've done this in the MyIndexedProperty constructor.

In use, the result is then:

IndexerClass indexedClass = new IndexerClass();
int someValue = indexedClass.IndexProperty[3, 4]; //returns 12

The main advantage of this approach is that you hide the actual implementation of how you are storing the values from the caller's use of the set and get methods.

You can also check values before deciding to proceed with the set operation e.g.

    public int this[int x, int y] {
        get
        {
            return Data[x, y];
        }
        set
        {
            if (value > 21) //Only over 21's allowed in here
            {
                Data[x, y] = value;
            }
        }
    }

Upvotes: 3

PProg
PProg

Reputation: 77

you can use list instead of array.

public Myclass
{
int a{get;set;};
int b{get;set;};
}

.....

public List<MyClass> myList{get;set;}

Upvotes: 0

Ron Sijm
Ron Sijm

Reputation: 8758

The thing with arrays is that you need to set their size before you can use them. For example, you would do something like this:

class MyOtherClass
{
    public MyOtherClass(int xMAx, int yMax)
    {
        MyProperty = new int[xMAx, yMax];
    }

    public int[,] MyProperty { get; private set; }
}

Your property does not need to expose a set method, because its not the MyProperty you're setting, but an internal value of MyProperty. For example, MyProperty[1,3].

Upvotes: 1

RJ Lohan
RJ Lohan

Reputation: 6527

You can bypass actually implementing the property and allow the compiler to do it for you, using auto properties;

public class Test
{
    // no actual implementation of myProperty is required in this form
    public int[,] myProperty { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        Test t = new Test();
        t.myProperty = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
        Console.WriteLine(t.myProperty[1, 2]);
        t.myProperty[1, 2] = 0;
        Console.WriteLine(t.myProperty[1, 2]);
    }
}

Upvotes: 3

Reed Copsey
Reed Copsey

Reputation: 564891

You can just expose the property getter, and use it:

class MyOtherClass
{
    public MyOtherClass()
    {
       myProperty = new int[3, 3];
    }

    public int[,] myProperty
    {
        get; private set;
    }
}

Upvotes: 5

Related Questions