So I have this two dimensional array in my class, and I'm trying to create an accessor group for it. But the compiler doesn't like what I'm trying to do at all.

private int[,] _resource = new int[5, 2];

public int Resource[int idx1, int idx2]
{
    get
    {
        return _resource[idx1,idx2];
    }
    set
    {
        _resource[idx1,idx2] = value;
    }
}

Not possible, or what am I doing wrong?

I want to put () somewhere in that code, but then that defeats the entire purpose of what I'm trying to do. :P

Edited 6 Years Ago by Zinderin: n/a

public int Resource(int idx1, int idx2)
{
    get
    {
        return _resource[idx1,idx2];
    }
    set
    {
        _resource[idx1,idx2] = value;
    }
}

Change it to this

I changed From

public int Resource[int idx1, int idx2]

to

public int Resource(int idx1, int idx2)

Edited 6 Years Ago by finito: n/a

You could move the array to its own class with an indexer then expose an instance of that class through your property:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //store instance of the class holding your array
        private Resource _resource = new Resource();

        //expose that instance via property
        public Resource resource
        {
            get { return _resource; }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //by accessing instance of indexed class via property you can
            //assign and retrieve values from property by index:
            this.resource[0, 0] = 1;
            this.resource[0, 1] = 2;
            int test = this.resource[0, 0] + this.resource[0, 1]; 
            //test = 3
        }
    }

    //class to store your array
    public class Resource
    {
        //private array
        private int[,] _resource = new int[5, 2];

        //access array through indexer
        public int this[int idx1, int idx2]
        {
            get { return _resource[idx1, idx2]; }
            set { _resource[idx1, idx2] = value; }
        }
    }
public int Resource(int idx1, int idx2)
{
    get
    {
        return _resource[idx1,idx2];
    }
    set
    {
        _resource[idx1,idx2] = value;
    }
}

That wouldnt work :/ By adding the parameters it will be treated as a method instead of a property and a method will not recognise the get/set syntax.

Edited 6 Years Ago by Geekitygeek: n/a

That wouldnt work :/ By adding the parameters it will be treated as a method instead of a property and a method will not recognise the get/set syntax.

Ahh yes I see my error. Sorry about that. It seems I didn't know what I was talking about. Thanks Ryshad.

No worries, we all make mistakes; The day you stop learning is the day you die, we're all here to help one another to learn :)

Comments
true

Well, I had to move forward so I made a GetRes() and SetRes() methods ... but I am still curious if it is possible ... there's just too many scenarios where a class will have a property that could be multi-dimensional.

However, Microsoft being Microsoft could have taken the position ... properties are not objects (which includes arrays) and just not provided for it.

I continued to play with it over the last couple of days everytime I got some wild idea, but I still haven't been able to get it to work as a true accessor.

Umm...did you overlook my post? I showed you how to use an indexer to produce the access you wanted. Granted, you have to move the array out into a seperate class, but the end result is what you asked for.

Comments
Absolutely right!

Umm...did you overlook my post? I showed you how to use an indexer to produce the access you wanted. Granted, you have to move the array out into a seperate class, but the end result is what you asked for.

No no, I didn't overlook your post Ryshad ... there's a couple of people up here who's opinion I always read ... and you're one of them.

Indexers (Indexing) isn't my problem. I actually understand indexing. What I was attempting to do is have a multi-dimensional array as a property.

I had found (on the web) how to have a simple array as a property, I was just trying to expand it to a multi-dimensional array.

In the end, I went with methods. But I didn't give up (just to learn) kept searching for a solution.

I even found another solution (like yours) where an indexed subclass containing an array property was inherited. That was crazy code, I could see all kinds of boxing problems with that solution. Yours was 'cleaner'.

However, just a few minutes ago, I discovered the Code Cops at Microsoft have laid down the law that properties should not return arrays because of code performance issues. And that makes sense because it gets us right back into a boxing situation I was trying to avoid in the first place. :)

So all is good, I'll just stick with what I have.

And as always, you guys rock!!!

Edited 6 Years Ago by Zinderin: n/a

Ah, OK..just checking you hadn't skipped over it :)
I'm glad you have found a solution that works for you..but just for my own piece of mind; what was the problem with the indexer method?
To my mind SomeClass.SomeArray[0,0] = 1; reads much better than SomeClass.SetValue(index1, index2, value); :p

This question has already been answered. Start a new discussion instead.