Pat Lindley
Pat Lindley

Reputation: 173

Mapping properties to array indexes

I have several properties in a class that appear as such:

property1_1 {get;set;}
property1_2 {get;set;}
property1_3 {get;set;}
...
property9_1 {get;set;}
property9_2 {get;set;}
property9_3 {get;set;}

These properties need to be mapped to indexes in an array such as:

array[0].property1 = property1_1
array[0].property2 = property1_2
array[0].property3 = property1_3
...
array[8].property1 = property9_1
array[8].property2 = property9_2
array[8].property3 = property9_3

There are around one hundred of these properties that need to mapped like this and I would rather not have to individually assign them via indexing. I've looked into using reflection and a couple of other ideas, but none have really "felt" better.

Any ideas?

Thanks.

Upvotes: 0

Views: 1447

Answers (1)

MethodMan
MethodMan

Reputation: 18843

Could you perhaps try something 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

Upvotes: 1

Related Questions