0

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!

4
  • 3
    TL;DR You should add code samples instead of a long explanation! :D Commented Oct 6, 2014 at 13:26
  • I didn't think I'd need to use code as it's a very simple (straight forward?) question. Commented Oct 6, 2014 at 13:30
  • @GhunerStrudel Then make your question simpler to read, if the issue is too simple!! Commented Oct 6, 2014 at 13:31
  • @GhunterStrudel A picture is worth a 1000 words, and a good SSCCE demonstrating the problem is worth a 1000 pictures. Commented Oct 6, 2014 at 13:34

4 Answers 4

3

Create an interface and an array of objects that implement that interface:

// choose a better name and make the properties read-only (get;} if necessary
public interface ILevelItem  
{
    public int XPos {get; set;}
    public int YPos {get; set;}
    public SolidBrush Brush {get; set;}
}

public class Wall : ILevelItem
{
    ....
}

public class Obstacle : ILevelItem
{
    ....
}

Now your "level grid" can be an array of the items

public ILevelItem[,] levelGrid;

and you can access the properties without casting:

int x = levelGrid[0,0].XPos;
Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

This sounds promising. I'll try that. Thanks mate!
It works. I gave your comment the mark as it was the first comment with the answer.
1

You should decorate your different objects with interfaces, i.e. IObstacle, IWall, IMonster, ITreasure... then you can easily test if objetsOfWorld[i] is an obstacle, or a wall.

Then you can access the needed property (x, y, brush).

If theses properties are shared by all objects, you may have a parent interface, IObjectOfMyWorld that exposes the 3 properties. Then your array will be an array of IObjectOfMyWorld.

Comments

1

I think you can't use "object" here, because it's the type of every object you create in your application. You should create an abstract class with all these properties (xpos etc.) or an interface. All other classes which uses these properties should then implement this interface or extend the abstract class.

abstract class YourAbstractClass
{
    public int XPos { get; set; } 
}

class WallObject : YourAbstractClass
{

}

Then you can have one List<YourAbstractClass> with all your objects.

You should search for the keyword inheritance ;)

1 Comment

I will remember that keyword. Thanks for the advice.

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.