Maryadi Poipo
Maryadi Poipo

Reputation: 1478

How to know childtype from It's parent in C#

I have parent class like this :

    class Alien
        {
            public String Name { get; set; }
            public bool IsAlive { get; set; }
            public Alien Parent { get; set; }
            public List<Alien> Children { get; set; }
            private bool statusGet
            public Alien(String name, bool isAlive, Alien parent = null)
            {
                Name = name;
                IsAlive = isAlive;
                Parent = parent;
                statusGet = false;
            }
           public void GiveBirth(IEnumerable<Alien> children)
            {
              if (Children == null)
                {
                   Children = new List<Alien>();
                }
                Children.AddRange(children);
          }

         public Alien Find(String name)
          {
           //some programming stuff

          }

    }

Then I have two child classes that inherit alien like these :

class BadAlien : Alien
    {
        int Kills { get; set; }
        public BadAlien(String name, bool isAlive, int kills, Alien parent = null)
            : base(name, isAlive, parent)
        {
            Kills = kills;
        }

    }



class GoodAlien:Alien
    {
        int Saves { get; set; }
        public GoodAlien(String name, bool isAlive, int saves, Alien parent = null)
            : base(name, isAlive, parent)
        {
            Saves = saves;
        }

    }

and then in myProgram, I'm making an alien object :

class Program
    {
        static void Main(string[] args)
        {
            Alien theFirstAlienEver = new Alien("Ace", false);
            theFirstAlienEver.GiveBirth(new List<Alien> { 
                new BadAlien("Bruce", true, 12),
                new GoodAlien("Bane", true, 3),
                new GoodAlien("Bill", false, 5)
            });
            theFirstAlienEver.Find("Bruce").GiveBirth(new List<Alien> { 
                new BadAlien("Charles", true, 15),
                new BadAlien("Cain", true, 17),
                new GoodAlien("Curt", false, 6)
            });
            theFirstAlienEver.Find("Bill").GiveBirth(new List<Alien> { 
                new GoodAlien("Christine", true, 2),
                new BadAlien("Connor", true, 11),
                new BadAlien("Chloe", false, 16)
            });
            theFirstAlienEver.Find("Connor").GiveBirth(new List<Alien> { 
                new GoodAlien("Daniel", true, 4),
                new BadAlien("Dylan", true, 12),
            });

}
}
}

So I have an object with some children withing it. Now from theFirstAlienEver object I want to know which one is BadAlien or GoodAlien from it's children, so How to achieve this...??? thanks a lot.. .:)

Note : I want to create a json string, but when accessing it's child, I can't define it's tipe, whether it's a BadAlien or GoodAlien. I hope this note make me point clear...:)

Upvotes: 0

Views: 71

Answers (2)

Zein Makki
Zein Makki

Reputation: 30042

You can use the is keyword :

An is expression evaluates to true if the provided expression is non-null, and the provided object can be cast to the provided type without causing an exception to be thrown.

foreach (var child in theFirstAlienEver.Children)
{
    if (child is GoodAlien)
    {
        // do something
    }
    else if (child is BadAlien)
    {
        // do something
    }
}

Upvotes: 2

samwise
samwise

Reputation: 2267

List<BadAlien> badAliens = theFirstAlienEver.Children.OfType<BadAlien>().ToList();
List<GoodAlien> goodAliens = theFirstAlienEver.Children.OfType<GoodAlien>().ToList();

Upvotes: 1

Related Questions