Hi,
i'm hoping somebody can help me out with this problem which has got me stumped (still new to C#)
In my console application, ive created a class called Map which contains an array of structs.
To access this array from the main class which contains main(), i attempted to write 'get/set properties' for the array, but it seems to have gone horribly wrong ![]()
====================================================================
Here is the struct:
struct positionStruct
{
public string location;
public int coordinateX;
public int coordinateY;
public int coordinateZ;
}
====================================================================
And here is the code in the class:
public class Map
{
positionStruct [] positionArray = new positionStruct[100];
// PROPERTY WITH GET AND SET FOR ARRAY, WHICH I CAN'T GET TO WORK
public positionStruct PositionArray[int index]
{
get
{
return positionArray[index];
}
set
{
positionArray[index] = value;
}
}
}
=========================================================
When i try to compile, the get/set bit receives a ton of errors, (e.g. ';' expected, ']' expected, and so on) so that is clearly wrong.
I'd be grateful if someone could point me in the right direction as to how to write the property,
thanks in advance !![]()
Tim
PS: in hindsight i realize i could probably write the program without resorting to an array of structs in a separate class, but now i'm determined to sort out this annoying problem!!![]()

Help! How to write 'get''set' property for an array of structs?
lax4u
hi,
the problem in set statment because you need to use 2 variables one is the index the other one is the value MyProperty[index] = value; , i don't know any property that take 2 values , in essense the property is consist of 2 methods getter and setter so you can use normal methods to do that instead of the property.
more over it will be better if you used the array index directly i don't know what is the difference that will make to write
Myproperty[index] = value;
or
MyArray[index] = value;
its the same, on the other hand the getter statment
variable = myarray[index]; or
varialbe = myproperty[index]
that doesn't save nothing
hope that helps
mghile
I would do it like this:
public
struct positionStruct{
public string location;
public int coordinateX;
public int coordinateY;
public int coordinateZ;
} public class Map
{
positionStruct [] positionArray = new positionStruct[100];
public positionStruct this[int index]
{
get { return positionArray[index]; }
set { positionArray[index] = value; }
}
}
That way you can access the array with Map[index]. Hope this helps.
/Erwin
*David*
Hi all,
Thanks for all the quick replies, they have really helped!!
I realized that Andreas was right about not needing the index in the Get/Set property, and have managed to sort everything out now.
=======================================
Anyways, here is the revised version of the code, (which is now error free
):
public struct positionStruct
{
public string location;
public int coordinateX;
public int coordinateY;
public int coordinateZ;
}
public class Map
{
positionStruct [] positionArray = new positionStruct[100];
// PROPERTY WITH GET AND SET FOR ARRAY
public positionStruct[] PositionArray
{
get {return positionArray;}
set {positionArray = value; }
}
}
==============================================
And this is an example of it being used in main :
Map map =
new Map() ; map.PositionArray[0].location = "Atomisation Point";Console.WriteLine("Point: " + map.PositionArray[0].location);
=============================
Once again, thanks everyone for helping!
Regards,
Tim
jnickfl1
You could just skip the index because it will work directly on the property.
positionStruct[] positionArray; // initialised in the constructor
public positionStruct[] PositionArray
{
get
{
return positionArray;
}
}
In code you can use it like
positionStruct pos = PositionArray[0];
PositionArray[1] = pos;