First, I create base class and inherited class
public class Entity
{
public int EntityID;
public EntityType type = EntityType.NONE;
}
public class EntityObject : Entity
{
public Vector2 position;
public EntityObject()
{
position = Vector2.Zero;
type = EntityType.OBJECT;
}
}
And then I create list of base class to store all inherited class
List<Entity> entityList = new List<Entity>();
entityList.Add(new EntityObject());
Later, I try to access a member of inherited class in list
for(int i = 0; i < entityList.Count; i++)
if(entityList[i].type == EntityType.OBJECT)
entityList[i].position = Vector2.One;
Yeah, I got an error : 'Entity' does not contain a definition for 'position'and no extension method 'position' accepting a first argument of type 'Entity' could be found (are you missing a using directive or an assembly reference?)
Is there a probably way to access inherited class properties from list of base class?
You need to cast the
Entity
instance to anEntityObject
instance, because thePosition
field is indeed not part ofEntity
So:
Will give you the correct type, which you can then access the
.Position
field against.As mentioned in the comments, you could also test the type / do the cast the following ways:
OR with C# 6
In the interests of mercy, as pointed out by Henk, you're clearly trying to include some type information in your class, in the form of the
EntityType
property.You can determine the type of an object at runtime:
Therefore:
will tell you the type of the instance, but using things like type checking of derived classes is often a smell.
Assuming you don't need any base functionality on
Entity
I would recommend going with an interface approach instead.I'd recommend looking here: Interface vs Base class