Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

Get methods of inheritance class

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.
[Image: jYK9GP1.gif]
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
(Jan 7, 2017 02:58 PM)TCP Wrote:  -snip-

Exactly what I needed. Thanks!
[Image: jYK9GP1.gif]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by lockheadApr 22, 2021 07:10 AM 0 147 Apr 22, 2021 07:10 AM
Last Post: lockhead
[C] by zNinjaApr 15, 2020 03:21 PM 0 117 Apr 15, 2020 03:21 PM
Last Post: zNinja
[Java] by CompSecDec 1, 2017 09:07 AM 7 262 Dec 1, 2017 09:07 AM
Last Post: CompSec