Beginner Inheritance Issue C# -
this question has answer here:
- property not exist in current context 4 answers
i'm learning c# , arrived @ inheritance part. first wrote following base class
class soldier { public int _health = 0; public int id = 0; public int level = 0; public string name = "soldier"; public void identify(int h, int id, int lvl, string n) { console.writeline("health: " + h + "\nid: " + id + "\nlevel: " + lvl + "\nname: " + n); } }
after of course wanted create simple subclass test out:
class knight : soldier { level = 2; public void ride(string name) { console.writeline(name + " can ride mount"); } }
but when run program gives me error "the name 'level' not exist in current context".
i know i'm doing wrong searched up, found cases users had no problem overriding property did, other added "base." before property. neither have worked.
thanks in advance.
you need code in class constructor, example:
class knight : soldier { //class constructor here public knight() { level = 2; } public void ride(string name) { console.writeline(name + " can ride mount"); } }
wiki
Comments
Post a Comment