Search code examples
c#.netmultidimensional-arrayvisual-studio-2013object-type

Getting varaibles from objects of different types inside an object array


So I got this small problem on getting variables from certain classes that are in a primitive multidimensional object array.

Simple explanation:

I got a primitive multidimensional object array which I'm using as a level grid. This array is filled with different types of objects to create a level (wall object, obstacle object etc.), so that's why I choose to use the "object" type..

Now all these types of objects have all three variables in common: an int xpos variable, an int ypos variable and a SolidBrush br variable.

What I'm using now is different Lists of the corresponding object type to get their positions and color for drawing. Now this is extra code of space which I don't actually need because every object is already in the main multidimensional array.

So how does one loop through a object array to get variables from different types of object classes that are inside this object array. Is it even possible as I'm using a global "object" type for my array?

It's a really simple question but I can't find an answer as I don't really know how to type the right keywords for the search. Thanks in advance.

EDIT: So I got it working thanks to using an Interface. Thanks everybody for your help and input. I would like to mark every interface answer but I can't so I'll mark the first comment. Sorry guys. Thank you all!


Solution

  • Make an interface with three properties: xpos, ypos, and brush. Have all your existing types that you put in the array implement this interface. Then change your multidimensional array type from Object to the new interface.