C
C#3d ago
Ahata

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 }
11 Replies
Joschi
Joschi3d ago
Would you please share a minimal example of the code, producing your problem?
Ahata
AhataOP3d ago
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.
ero
ero3d ago
what you posted is still not valid code, also $code
MODiX
MODiX3d ago
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/
ero
ero3d ago
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.
class Base
{
private string _a;
private string _b;

public Base(string a, string b)
{
_a = a;
_b = b;
}
}

class Child : Base
{
public Child(string a, string b)
: base(a, b) { }
}
class Base
{
private string _a;
private string _b;

public Base(string a, string b)
{
_a = a;
_b = b;
}
}

class Child : Base
{
public Child(string a, string b)
: base(a, b) { }
}
Joschi
Joschi3d ago
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.
c#
using System;

public class Program
{
public static void Main()
{
Base baseObject = new Child("Value for base", "Value for child");

Console.WriteLine(baseObject.BaseProperty);
}
}

class Base
{
public string BaseProperty { get; set; }

public Base(string baseProperty)
{
BaseProperty = baseProperty;
}
}

class Child : Base
{
public Child(string baseProperty, string childProperty) : base(baseProperty)
{
ChildProperty = childProperty;
}

public string ChildProperty { get; set; }
}
c#
using System;

public class Program
{
public static void Main()
{
Base baseObject = new Child("Value for base", "Value for child");

Console.WriteLine(baseObject.BaseProperty);
}
}

class Base
{
public string BaseProperty { get; set; }

public Base(string baseProperty)
{
BaseProperty = baseProperty;
}
}

class Child : Base
{
public Child(string baseProperty, string childProperty) : base(baseProperty)
{
ChildProperty = childProperty;
}

public string ChildProperty { get; set; }
}
Ahata
AhataOP3d ago
I'm trying this but I am still getting null :/ also sorry for the code format
Joschi
Joschi3d ago
Ok found the problem and it has nothing to do with inheritance.
c#
private Texture2D ltexture;

public Texture2D texture
{
get{ return ltexture;}
set{ ltexture = texture;}
}

public Entity(Texture2D texture, Vector2 position)
{
this.position = position;
this.texture = texture;
}
c#
private Texture2D ltexture;

public Texture2D texture
{
get{ return ltexture;}
set{ ltexture = texture;}
}

public Entity(Texture2D texture, Vector2 position)
{
this.position = position;
this.texture = texture;
}
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.
c#
private Texture2D ltexture;

public Texture2D texture
{
get{ return ltexture;}
set{ ltexture = value;}
}

public Entity(Texture2D texture, Vector2 position)
{
this.position = position;
this.texture = texture;
}
c#
private Texture2D ltexture;

public Texture2D texture
{
get{ return ltexture;}
set{ ltexture = value;}
}

public Entity(Texture2D texture, Vector2 position)
{
this.position = position;
this.texture = texture;
}
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.
c#
class Entity
{
// Auto Property
public Vector2 Position {get; set;}

// Backing Field
private Texture2D _texture;

// Property with Backing Field
public Texture2D Texture
{
get{ return _texture;}
set{ _texture = value;}
}

public Entity(Texture2D texture, Vector2 position)
{
Position = position;
Texture = texture;
}
}
c#
class Entity
{
// Auto Property
public Vector2 Position {get; set;}

// Backing Field
private Texture2D _texture;

// Property with Backing Field
public Texture2D Texture
{
get{ return _texture;}
set{ _texture = value;}
}

public Entity(Texture2D texture, Vector2 position)
{
Position = position;
Texture = texture;
}
}
Ahata
AhataOP3d ago
Oh wow That's amazing thank you so much.
Anton
Anton3d ago
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 point
Ahata
AhataOP2d ago
I am trying to make a game using monogame, so I learn as I go thanks for the info
Want results from more Discord servers?
Add your server