0

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.

5
  • I believe your best bet is reflection, was there a specific reason you didn't choose reflection? I guess you could write a quick script to auto-gen all of the assignments but I guess that isn't really the point... Commented Dec 6, 2011 at 14:52
  • I'm not sure why "feelings" matter very much for the reflection solution. That is the easiest, most direct route. Commented Dec 6, 2011 at 14:53
  • What do you mean exactly? Since you could combine three properties within a struct or class. If you would then generate an array where the elements are of this type. Or do I see this the wrong way? Commented Dec 6, 2011 at 14:55
  • reflection will probably be the way i ultimately go to refactor this. While I'm not overly concerned about performance, my thinking was having to reflect across all of these properties didn't seem as effecient as just directly assigning them via indexing. Reducing the lines of code? yes. performing any better? not sure. Commented Dec 6, 2011 at 15:15
  • sorry its taken me so long to return to this. I've gone ahead and implemented reflection to address this issue. The final reason was simply to remove the preponderance of code. Measurable performance wasn't impacted at all. Honestly, I should've just done this from the beginning. Commented Dec 29, 2011 at 21:56

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.