Inheritance
Hello all,
I have an inheritance problem. When I create an object Entity x; and later I asign him the value x = new MovingEntity(Texture,Position), the values are not assigned onto x. Why?
class Entity
{
#region Values
private Vector2 lposition;
public Vector2 position
{
get{ return lposition;}
set{ lposition = position;}
}
private Texture2D ltexture;
public Texture2D texture
{
get{ return ltexture;}
set{ ltexture = texture;}
}
#endregion Values
#region Constructors
public Entity(){}
public Entity(Vector2 position)
{
this.position = position;
} public Entity(Texture2D texture) { this.texture = texture; } public Entity(Texture2D texture, Vector2 position) { this.position = position; this.texture = texture; } #endregion Constructors #region Functions #endregion Functions } class MovingEntity : Entity { #region Values private float lspeed; public float speed { get{ return lspeed; } set{ lspeed = speed; } } #endregion Values
#region Constructors public MovingEntity(){} public MovingEntity(Texture2D texture,Vector2 position) : base(texture,position){} #endregion Constructors }
} public Entity(Texture2D texture) { this.texture = texture; } public Entity(Texture2D texture, Vector2 position) { this.position = position; this.texture = texture; } #endregion Constructors #region Functions #endregion Functions } class MovingEntity : Entity { #region Values private float lspeed; public float speed { get{ return lspeed; } set{ lspeed = speed; } } #endregion Values
#region Constructors public MovingEntity(){} public MovingEntity(Texture2D texture,Vector2 position) : base(texture,position){} #endregion Constructors }
11 Replies
Would you please share a minimal example of the code, producing your problem?
class Base{
private string a;
public Base(string a,string b){
this.a = a;
this.b = b;
}
}
class Child : Base {
public Child(string a,string b){
this.a = a;
this.b = b;
}
}
Main{
Base x = new Child(y, z);
//here x does not get assigned y and z so x.y and x.z are null
}
Does this help?
My problem is that when I create a Base object as a Child, the values passed are returned as null.
what you posted is still not valid code, also $code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
For longer snippets, use: https://paste.mod.gg/but i assume what you mean is that
a
and b
aren't passed down to Base
? you probably want to call the base constructor.
He calls the base constructor in his first example so I guess it was a omission.
The code won't even compile without.
The following works as expected.
I'm trying this but I am still getting null :/
also sorry for the code format
Ok found the problem and it has nothing to do with inheritance.
Your
constructor
sets the value of the texture
property
, but your setter
sets its value to itself.
The setter
has to set the value of the backing field
(ltexture
) to value
.
While we are at it. You should adhere to the C# format standards. Properties
start with uppercase letters and fields
start lowercase and often even with a underscore.
And if you don't have a special reason for creating a backing field yourself you should use auto properties
.
Oh wow
That's amazing thank you so much.
You'll hit a brick wall with inheritance at some point. Look into component architecture.
Unless it's for learning inheritance, those constructors are useless, and so is encapsulation at this point
Look into nullable types and
required
at some pointI am trying to make a game using monogame, so I learn as I go
thanks for the info