0

I believe this question is fairly basic but I am having trouble finding an answer to this question. In C# let's say I have 3 classes: A, B, C

B derives from A

C derives from B

Now, if I wanted a list or array of objects of type class A but wanted the array to be able to house objects of type B and C this is no problem... I could do something like this:

A[] myArrayofAtypes;

However let's say I make the first element of this array of type C. If type C has a variable defined in its class definition that ONLY exists in class C's class definition... how do I access that variable from the array? I can't just do A[0].MyVariableGetter as that variable does not exist in the base class, A.

Any suggestions?

Brad Mash
  • 69
  • 6
  • creating an array of A[] and holding everything there isn't polymophism – ro-E Mar 15 '15 at 17:17
  • My question had factors pertaining to polymorphism so I figured marking that as a category for the question was appropriate. Also, I never said creating an array of A[] and holding everything there was polymophism. – Brad Mash Mar 15 '15 at 17:52
  • your question is about casting and inheritance.. once you check the object's type you aren't polymorphic – ro-E Mar 15 '15 at 19:20
  • Would it make you feel better if I said inclusion polymorphism? – Brad Mash Mar 15 '15 at 19:28

3 Answers3

1

You have to downcast it:

C c = (C)myArrayofAtypes[0];
var v = c.MyVariableGetter;
dario
  • 5,149
  • 12
  • 28
  • 32
  • Is downcasting processing intensive if it is done a lot? I mean, imagine an array of 100 elements and every element needs downcasted all on succession to access their details... Is this a bad thing for processing time / resources? – Brad Mash Mar 15 '15 at 17:17
  • 100 elements? nope. More than few ten thousands? Possibly. – mcy Mar 15 '15 at 17:19
  • @BradMash check [this](http://stackoverflow.com/questions/305755/what-is-the-performance-hit-with-downcasting). – dario Mar 15 '15 at 17:19
  • If I implement what you did king.code then to actually update the array element so it gets the updated object at position [0] in the array do I need to then do myArrayofAtypes[0] = c ? Or do I have to upcast again and then do myArrayofAtypes[0] = c ? – Brad Mash Mar 15 '15 at 17:31
  • @BradMash since your array holds A references you have to always downcast. I mean, if you do this `myArrayofAtypes[0] = (C)myArrayofAtypes[0];`, you are downcasting and then upcasting again. – dario Mar 15 '15 at 17:34
  • I guess what I mean is... C c = (C)myArrayofAtypes[0]; c.MyVariableGetter = 15; myArrayofAtypes[0] = c; // if this does not work, do I need to do this: // myArrayofAtypes[0] = (A)c; – Brad Mash Mar 15 '15 at 17:39
  • @BradMash upcasting is implicit, you can do it but you don't need it. – dario Mar 15 '15 at 17:40
  • Thanks man I really appreciate your help... Working with ColdFusion all day every day has made my OOP a little rusty. Appreciate your time – Brad Mash Mar 15 '15 at 17:42
  • @BradMash In your example you don't need to do `myArrayofAtypes[0] = c;`, because you only modified a property of `c` the `myArrayofAtypes[0]` was also modified because both variables `c` and `myArrayofAtypes[0]` point to the same object (Unless `A`, `B`, and `C` are stucts instead of classes, but that would mean you have a mutable stuct, which should not be done.) – Scott Chamberlain Mar 15 '15 at 18:45
0

You have to type cast it to type c and access the member.

If (arr[i] is c)
{
    ((C)arr[0]).member = value;
 }
user92454
  • 1,201
  • 1
  • 10
  • 13
0

Instead of casting as 'king.code' has suggested like this I would use these pretty C# operators which are 'is' and 'as'.

public void CallIfTypeC(A a)
{
    if (a is C)
    {
        C c = a as C;
        c.DoAction();
    }
}
Huhra
  • 92
  • 4
  • That makes sense but let me ask you this... can I do the downcasting, update A[0] in my example (update this instance's class C specific variables) and have that instance updated in the array? Does that make sense? – Brad Mash Mar 15 '15 at 17:21