3

I am trying to create a base class in c# that I can extend out to sub classes.

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 exists and property of star, it exists as star.star.type but completely inaccessable, or I can not figure out how to access it.

I do not know if I'm extending the ObjectsInTheSky property properly or not. Any help or pointers will be greatly appreciated.

2
  • 1
    Your Stars class only has a parameterless constructor, provided by default by the compiler. Commented Jan 21, 2013 at 17:24
  • Just a point for the future, @user1224302, that the code for Load wasn't required (i.e. just put something like /* stuff loaded here */ inside the body of the method) - people don't always bother to scroll down code examples (even though we really should). That said, though, it's a damn site better to actually have code than none at all - so you've already done 100% better than 50% of new users ;) Commented Jan 21, 2013 at 17:59

2 Answers 2

6

It looks as though you are trying to use a constructor that is not defined on your subclass Stars or the base class.

Stars star = new Stars(142);

If you are trying to use the .Load(int) method then you would need to do this:

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

Or, if you are trying to use the base constructor, you need to define it in the subclass:

public class Stars : ObjectsInTheSky 
{
    public Stars(int id) : base(id) // base class's 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}
}
Sign up to request clarification or add additional context in comments.

5 Comments

He probably just wants to add the additional constructor to all child types, since Load probably shouldn't be public.
...Or add an int constructor in Stars that channels to the same in ObjectsInTheSky
Thanks, I added the creation of the sub type's constructor as well.
cool +1 then - although worth pointing out then that if that updated code is followed, then your second code block can't be used unless a default constructor is also added :)
@AndrasZoltan Again, thanks, added another constructor and comment.
2

Constructors in C# are not inherited. You need to add the 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 just calls the base class's constructor for you.

Comments

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.