I need to store an array with 5 directions, and I wonder what would be the most readable way.
I could do array
int myDirection[100] = {0,1,2,3,4,5,4,4,3,4,0......etc // poor readability
Or I could use constants and do
const up = 1;
const down = 2; //... etc
int myDirection[100] = {up, down, center, right, left, .....etc
Or I could write a struct
struct direction
{ private byte mydirectionval;
{
public void direction Setup(){mydirection =1;}
public void direction Setright(){mydirection=2;}
public void direction Setdown(){mydirection=3;}
public void direction Setleft(){mydirection=4;}
public void direction Setcenter(){mydirection=0;}
public direction Getdirection(){....
}
direction newpath[100] = { ......
Or should I write a class
public static class direction // not sure use static here ?
{ private int direction =0;
public const int Up = 1;
public const int left =2;
public const int down = 3;
public const int right = 4;
public const int center = 0;
public int getdirection(){return direction}
public void int setdirection(int x){direction=x}
}
direction mydir[100] = new direction;
mydir[0].setdirection(up);
// or use it like
mydir[0]=mydir.up //==> i prefer readability of code so this looks better?
OK people sugest to use enum, but then i need an array mydir[100] of enums
how to do that ??
Use an enum.
You can use it in an array:
or