Hack Forums
[C#] Get methods of inheritance class - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Get methods of inheritance class (/showthread.php?tid=5517043)



Get methods of inheritance class - JuicyProgramming - Jan 7, 2017

Hello,

I am building an application for my school project and I am trying to loop through a list of Cars.

A BMW is a Car.

I am looking to do something with the attribute I have in the BMW class but I am looping through a list cars so I am unable to get the attribute only the BMW has.

if (_carList.ElementAt(idx) is BMW)

then do something with that BMW object.

The Car class only has 2 attributes but the BMW class has another one. I am trying to get the specific one for the BMW but I am only able to get the 2 car attributes. I am sure there is a quick fix for this.

Anyone? Thanks :)
Have a nice day.


* - TCP - Jan 7, 2017

Supposing you have a situation like this
Code
class Obj
{
    public string p1, p2;
}

class Obj2 : Obj
{
    public string p3;
}

When you check if Obj is also Obj2 you can simply alter the casting
Code
if (olist.ElementAt(idx) is Obj2)
    {
        var o = (Obj2)olist.ElementAt(idx);
        Console.WriteLine(o.p3);
    }

And you should have an Obj2 with his properties


* - JuicyProgramming - Jan 7, 2017

(Jan 7, 2017 02:58 PM)KitKat707 Wrote:  -snip-

Exactly what I needed. Thanks!