Well, it’s hard to explain it in words. So here’s an example. In the SDL .NET API, there’s a class called SpriteCollection which is designed to be, well… a collection of sprites. However the problem is that for some reason the developers decided to not make SpriteCollection generic, like the .NET List<> class. So, I guess, it can only work with the Sprite class or other classes that inherit from Sprite? Anyway, let’s say I derive Tile from Sprite. And I add two new properties to it:
class Tile : Sprite
{
public Tile(char symbol = default(char), bool solid = true)
{
Symbol = symbol;
Solid = solid;
}
public char Symbol
{
get;
set;
}
public bool Solid
{
get;
set;
}
}
Now, how can I use the SpriteCollection with Tiles and still be able to use the Tile exclusive properties like Solid?
One (probably stupid) solution would perhaps be this:
Tile t = (Tile) myCollection[i];
if (t.Solid) { ... }
Is there any better way to do something like this?
Your proposal to cast the return values of the
SpriteCollectiontoTileis the way to do it. You do not have better options if you need to use the collection class provided and cannot change it.