C # Object Inheritance

I am trying to create a base class in C # that I can extend to subclasses.

For example:

public class ObjectsInTheSky 
{
    public string Size, Shape;
    public float Mass;
    public int DistanceFromEarth;
    public bool hasAtmosphere, hasLife;
    public enum ObjectTypes {Planets,Stars,Moons}

    public ObjectsInTheSky( int id ) 
    {
        this.Load( id );
    }
    public void Load( int id) 
    {
        DataTable table = Get.DataTable.From.DataBase(id);

        System.Reflection.PropertyInfo[] propInfo = this.GetType().GetProperties();
        Type tp = this.GetType();
        foreach (System.Reflection.PropertyInfo info in propInfo)
        {
            PropertyInfo p = tp.GetProperty(info.Name);
            try
            {
                if (info.PropertyType.Name == "String")
                {
                    p.SetValue(this, table.Rows[0][info.Name].ToString(), null);
                }
                else if (info.PropertyType.Name == "DateTime")
                {
                    p.SetValue(this, (DateTime)table.Rows[0][info.Name], null);
                }
                else
                {
                    p.SetValue(this, Convert.ToInt32(table.Rows[0][info.Name]), null);
                }
            }
            catch (Exception e) 
            {
                Console.Write(e.ToString());
            }
        }
    }
}

public class Planets : ObjectsInTheSky 
{
    public Moons[] moons;
}

public class Moons : ObjectsInTheSky 
{

}

public class Stars : ObjectsInTheSky 
{
    public StarTypes type;
    public enum StarTypes {Binary,Pulsar,RedGiant}
}

      

My problem is when I try to use an object:

Stars star = new Stars(142);

      

star.type does not exist and the star property, it exists as star.star.type, but is completely unavailable, or I cannot figure out how to access it.

I don't know if I am extending the ObjectsInTheSky property correctly or not. Any help or pointers would be greatly appreciated.

+3


source to share


2 answers


It looks like you are trying to use a constructor that is not defined in your subclass Stars

or base class.

Stars star = new Stars(142);

      

If you are trying to use a method .Load(int)

, you will need to do this:



Stars star = new Stars();
star.Load(142);

      

Or, if you are trying to use a basic constructor, you need to define it in a subclass:

public class Stars : ObjectsInTheSky 
{
    public Stars(int id) : base(id) // base class constructor passing in the id value
    {
    }

    public Stars()  // in order to not break the code above
    {
    }

    public StarTypes type;
    public enum StarTypes {Binary,Pulsar,RedGiant}
}

      

+6


source


Constructors in C # are not inherited. You need to add additional constructor overloads to each of the base classes:

public class Stars : ObjectsInTheSky 
{
    public Stars(int id) : base(id) { }

    public StarTypes type;
    public enum StarTypes {Binary,Pulsar,RedGiant}
}

      



This will create a constructor that will simply call the base class constructor for you.

+2


source







All Articles