-1

I'm writing a simple rpg game to get some practice in C# and i got a problem. I can't get access to a child parameter while im using foreach. Is there a way not to use two different lists for each type and remain on list with base type?

class A{}
class B : A{public int hp=5}
class C : A{public int hp=10}
List<A> d = new List();
d.Add(new B());
d.Add(new C());
foreach(A a in d){
    a.hp--;            //does not see 'hp'
}
  • 1
    Read this whole [series](https://ericlippert.com/2015/04/27/wizards-and-warriors-part-one/), it will help you start on the right track with c# and rpg games. – InBetween Jan 12 '19 at 21:28
  • Answering your question, the type `T` of `List` should be a type that you can use to leverage the desired commonality, otherwise your polymorphism is useless. In your case, `A` doesn't seem to be the right type. You are either missing a common base type or interface for `B` and `C`. – InBetween Jan 12 '19 at 21:31
  • Variable `a` is of type `A`. Does `A` has a `hp` property? No... If you want that `A` (and all types derived from `A`) should have a public `hp` property, make `hp` a property of `A`, and not of `B` or `C`... –  Jan 12 '19 at 21:31
  • @elgonzo 'A' has alot of important stuff in my project including 'hp', that was just an example. – Danchyg1337 Jan 12 '19 at 21:34
  • 1
    Your question clearly shows that `A` does NOT have a `hp` property/field. Also, the error/problem you mentioned that `a.hp--` cannot be "seen" indicates that `A` does NOT have a `hp` property/field (or the property is perhaps protected/private). If you ask a question about some of your code, your code example(s) in your question should reflect the reality of your code. Avoid like the plague creating a scenario in your example code that significantly differs from your real code with respect to the context of the question. –  Jan 12 '19 at 21:37
  • It would be awesome if you could provide a [mcve] that relates directly to your question. – mjwills Jan 12 '19 at 21:42
  • @elgonzo The reason why i not mentioned `hp` in `A` is because `a.hp--` will decrease hp from a base class, but for my purposes it should be a child class which i added early in list. – Danchyg1337 Jan 12 '19 at 21:45
  • You will need to make your question and problem clear in the question. What you just told here in the comments **needs** to be part of your question. The fact that A has an hp declaration, and B and C have their own hp declarations, and that you want manipulate hp of B or C but not hp of A is a fundamental core aspect of your problem. You cannot expect people to give helpful advice if you hide the real problem from them. Thus, **edit** and improve your question to describe your actual real problem ;-) –  Jan 12 '19 at 21:48
  • That said, why would you have separate `hp` property/field declaration in every single class A,B,C to begin with? What's the point of doing that, what did you try to achieve? It sounds really like only the common base class `A` should declare the `hp` property/field (with `B` and `C` simply inheriting it). If you disagree with this, then why exactly do you not want let `B` and `C` inherit the `hp` property/field? –  Jan 12 '19 at 21:49

1 Answers1

1

A solution would be to have B and C implement a common interface and then change the list to be of that type.

e.g.

public interface IHasHealth
{
    int hp { get; set; }
}

public class A{}

public class B : A, IHasHealth
{
    public int hp { get; set; } = 5;
}

public class C : A, IHasHealth
{
    public int hp { get; set; } = 10;
}

public void Main(){
    List<IHasHealth> d = new List<IHasHealth>();
    d.Add(new B());
    d.Add(new C());
    foreach(IHasHealth a in d){
        a.hp--;
    }
}
sedders123
  • 791
  • 1
  • 9
  • 19