XNA Creators Club Online
Page 1 of 1 (8 items)
Sort Posts: Previous Next

Overriding an inherrited constuctor

Last post 3/6/2008 6:53 AM by RoyalFraggy. 7 replies.
  • 3/3/2008 10:40 PM

    Overriding an inherrited constuctor

    I have two classes inherriting from GameObject, which has a constructor. I want to give the inherriting classes their own constructors, but I keep getting the error: "No overload for method 'GameObject' takes '0' arguments." Is there something I'm not getting here?
  • 3/3/2008 10:59 PM In reply to

    Re: Overriding an inherrited constuctor

    Answer
    Reply Quote
    Maybe this is what you need?


    public class GameObject
    {
    public GameObject(string myName)
    { ...}
    }


    So game object has a constructor requiring 1 argument.

    Next you'll need another object, let's call it CarObject:GameObject
    public class CarObject: GameObject
    {
    //no argument in this constructor
    public CarObject():base("myCar")
    {
    ...
    }
    //you can also do it like this
    public CarObject(string myName):base(myName)
    {
    ...
    }
    //you can also do it like this
    public CarObject(string myName, Color myColor):base(myName)
    {
    this.Color = myColor;
    }
    }

    This will pass in "myCar" as the required myName argument of the base class's constructor.


  • 3/3/2008 11:06 PM In reply to

    Re: Overriding an inherrited constuctor

    I would like to recommend you take a bit to go through some of the tutorials here: http://www.csharp-station.com/Tutorial.aspx. Getting a good grounding in C# will definitely help you with any of the errors you are having like this. Trying to learn XNA without first learning C# is much like trying to run before learning to crawl. It's possible, but it's more work than simply learning C# first and then learning XNA.
  • 3/3/2008 11:13 PM In reply to

    Re: Overriding an inherrited constuctor

    I highly recomend following Nicks advice.

    And, to avoid the error you are having, I think you must be sure you parent class has a constructor without arguments.

        public class GameObject
    {
    public GameObject()
    {
    }

    public GameObject(int someInt)
    {
    }

    ...as many constructors as you need

    }

    public class ExtendedGameObject : GameObject
    {
    public ExtendedGameObject()
    {
    }

    public ExtendedGameObject(float someFloat)
    {
    }

    ...as many constructors as you need

    }
  • 3/3/2008 11:23 PM In reply to

    Re: Overriding an inherrited constuctor

    Cosmos:

    And, to avoid the error you are having, I think you must be sure you parent class has a constructor without arguments.



    That's one way. Or check out Jim's post above. That's the other way to handle it.
  • 3/4/2008 8:47 AM In reply to

    Re: Overriding an inherrited constuctor

    Thanks guys. Sorry for asking such a basic question.
  • 3/4/2008 11:57 AM In reply to

    Re: Overriding an inherrited constuctor

    TEMPVS MORTIS:
    Thanks guys. Sorry for asking such a basic question.


    Nothing wrong with asking questions. Please check out that link I gave above, though. It will help you with lots of these C# questions and problems thus getting you into making your game faster. :)
  • 3/6/2008 6:53 AM In reply to

    Re: Overriding an inherrited constuctor

    I actually ran into this last week.
    Making an empty constructor solves the problem indeed but I  think the correct way of implementing would be to make the empty constructor "protected" instead of public, so it cant be accessed incidentely.
Page 1 of 1 (8 items) Previous Next