c# - Unsure about parameters when calling base constructor from derived class -


i having troubles implementing inheritence game. thought best introduce inheritence, make code neater , organised in logical way.

the problem facing atm parameters when calling constructor base class in derived class.

i using paddle class base class , player class class inheriting paddle.

the code shown below:

in paddle class....

//properties    protected texture2d ptexture;   protected vector2 pposition;   protected vector2 pvelocity;   //constructor paddle class (only first line here, rest irrelevant)  public paddle(texture2d newtexture, vector2 newposition, viewport theviewport, color newcolour) 

in player class....

//properties playerindex pid;   //constructor player class (only first line here, rest irrelevant) public player(playerindex newid) : base() 

before mention reason why not working because base constructor im calling in player class doesn't have parameters, whereas constructor in base class/paddle class does, understand that. problem facing setting texture, position, etc of player texture, position, etc in base class, common properties player , enemy class (another class inherits paddle it's similar player, hence mentioning now). don't know put parameters when calling base constructor in player class.

thanks reading , in advance :-)

added code i've changed below:

public player(playerindex newid, texture2d newtexture, vector2 newposition, viewport theviewport, color newcolour)         : base(newtexture, newposition, theviewport, newcolour) 

to use constructor in way, have give derived constructor of information needed base constructor, can call so:

public player(playerindex newid, texture2d newtexture, vector2 newposition, viewport theviewport, color newcolour) 

you pass base:

public player(playerindex newid, texture2d newtexture, vector2 newposition, viewport theviewport, color newcolour) : base(newtexture, newposition, theviewport, newcolour); 

obviously makes player constructor quite bit larger. avoid problem, declare init() function on base class (non-overridable) takes parameters constructor does, , have owning class call after instantiation. either way, have give class information eventually.

let me know if can clarify or further!.

update

  1. yes, :base(...) syntax same thing writing (if valid c#) mybaseclasspointer = new base(...); (where base paddle in case). again, not valid c#. have use :base instantiate base class when using non-default constructor. sets properties same way if had instantiated directly, , long not marked private, available derived class (child bit of misnomer, since term refers composition).

  2. i'm not recommending use of init() function, using class isn't guaranteed call it. being said, work pretty so:

    class player {    private bool initialized = false;    public player()    {}     public void init(texture2d newtexture, vector2 newposition, viewport theviewport, color newcolour)    {       //all stuff constructor used       initialized = true;    }     public void somefunctionusingvariables()    {    if (initialized)    { //do whatever }    } } 

the advantage don't have pass data init() needs player constructor, knowledge of these fields doesn't need exist in player class itself. works, long if checks ok , remember call init in instantiating class.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -