c# - Need help to understand polymorphism -


the classes below consist of

a - father class

b - child class

holder - contains list of a's

i want reach child property list of fatherobjects. why cant this? or better question, how do this?

public class {     public int var = 0; }  public class b : {     public int property1 { get; set; }     public int property2 { get; set; }      public b()     {      }      public b(b p_b)     {         property1 = p_b.property1;         property2 = p_b.property2;     } }  class holder {     private list<a> m_objects = new list<a>();      public void addobject(a p_object)     {         m_objects.add(p_object);     }      public void addobjectproperty1(b p_b)     {         // @ point, m_objects holds b-object. , want add value property1         // there no property1 in a-class cant this. how use base.values          // statement 1 below?         int index = m_objects.findindex(item => item.property1 == p_b.property1);         if (index > -1)             m_objects.elementat(index).property1 += p_b.property1;     } }   class program     {         static void main(string[] args)         {             // class hold objects             holder h = new holder();              // create b object             b b = new b();             b.property1 = 1;             b.property2 = 2;              // place new instance of b-object in list of a's             h.addobject(new b(b));              // add value property1 value in b-object in a-list.  :p             h.addobjectproperty1(b);              console.writeline(++b.var);             console.readline();           }     } 

you can use type casting:

(m_objects[i] b).property1 

or

((b)m_objects[i]).property1 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -