# Using a custom struct as a 2D array address

I’ve created this simple struct:

``````public struct V2int {
public int x;
public int y;
public V2int( int x, int y ) {
this.x = x; this.y = y;
}
}
``````

If I’ve created a 2D array of bools, say:

``````bool[,] foo = new bool[5,5];
``````

And I use my struct thus:

``````V2int point = new V2int(2,4);
``````

I know I can find the bool at that grid point using:

``````bool check = foo[point.x, point.y];
``````

But is there any way of using something like:

``````bool check = foo[point]; // this throws an error
``````

Well, you could if you made a wrapper class in C# that exposed an overloaded indexer that did something like this:

``````public bool this[V2int index]
{
return innerArray[index.x, index.y];
}
``````

not really sure what you are trying to make with this pattern? can you explain it a bit more? I think you misuse the struct, if you place a function inside it. A struct is supposed to be pure data.

if you want easy access to 2D data in an array, you could make it single dimensioned and multiply the row manually like its done in many old retro tilebased games.

Instead of Array[xPos, yPos] you can use Array[xPos + (xMax*yPos)] where xMax is the dimension of X

You could also incapsulate the Check function by writing a Get function for checking if true or false and then place the above check inside that function.

``````bool Checkfoo(int x, int y)
{
return barArray[x + (y*xMax)];
}
``````

This will leave you with a simple:

``````if (Checkfoo(x,y)) {do something}
``````

If this doesnt work, leave me a comment on what you are trying and then we can fix it together.

Macfanpro (correct answer here) put me on the right track towards indexers… here is a complete code example that fully illustrates how they work.

``````using UnityEngine;
using System.Collections;

public class Indexers : MonoBehaviour {

MyClass test = new MyClass();

void Start () {

// Fill test.str with strings identifying the array location
for (int a=0; a<5; a++) {
for (int b=0; b<5; b++) {

string s = string.Format("String {0},{1}",a,b);

// The normal way
test.str[a,b] = s;

// Using indexers
test[a,b] = s;

// Using indexers and the int2d struct
int2d ab = new int2d(a,b);
test[ab] = s;

}
}

}

void Update () {

// Make a random index
int x = Random.Range(0,5);
int y = Random.Range(0,5);

string s = string.Format("At Index {0},{1}: ",x,y);

// The normal way
s += "(normal)\""+test.str[x,y]+"\";";

// Using indexers
s += "(indexer)\""+test[x,y]+"\";";

// Using indexers and the int2d struct
int2d xy = new int2d(x,y);
s += "(indexer+struct)\""+test[xy]+"\"";

Debug.Log( s );

}

}

// Struct containing a 2d integer location
public struct int2d {

public int x,y;
public int2d(int xx, int yy) { this.x=xx; this.y=yy; }

}

// Simple class that contains a 2D array of strings. Could be anything...
public class MyClass {

public string[,] str;

public MyClass() {
str = new string[5,5];
}

// Indexer for [int,int]
public string this[int x, int y]
{
get
{ return str[x,y]; }
set
{ str[x,y] = value; }
}

// Indexer for [int2d]