Max Skliar
Max Skliar

Reputation: 61

C# Counting custom class objects in the custom class array

I'm new to c# and programming at all. I have a task to create a class "Animals" which is a parent for "Predator" and "Herbivorous" and these 2 are parents to "Wolf", "Fox", "Hare", "Deer" respectively. So I did that. Next step is to create an array of these objects and count how many predators and herbivorous animals are there in array. And this is the spot where the problem occurs. (Functionality of the classes can be any)

My classes:

public abstract class Animal
{

}

public abstract class Predator : Animal
{
    protected string predatorName;
    public Predator (string typeOfAnimal)
    {
        this.predatorName = typeOfAnimal;
    }
}

public abstract class Herbivorous : Animal
{
    protected string herbivorousName;
    public Herbivorous (string typeOfAnimal)
    {
        this.herbivorousName = typeOfAnimal;
    }
}

public class Wolf : Predator
{
    protected string wolfName;
    public Wolf (string typeOfAnimal, string nameOfTheWolf) : base(typeOfAnimal)
    {
        this.wolfName = nameOfTheWolf;
    }
}

public class Fox : Predator
{
    protected string foxName;
    public Fox(string typeOfAnimal, string nameOfTheFox) : base(typeOfAnimal)
    {
        this.foxName = nameOfTheFox;
    }
}

public class Hare : Herbivorous
{
    protected string hareName;
    public Hare(string typeOfAnimal, string nameOfTheHare) : base(typeOfAnimal)
    {
        this.hareName = nameOfTheHare;
    }
}

public class Deer : Herbivorous
{
    protected string deerName;
    public Deer(string typeOfAnimal, string nameOfTheDeer) : base(typeOfAnimal)
    {
        this.deerName = nameOfTheDeer;
    }
}

My main:

class Program
{
        static void Main(string[] args)
        {                
            Problem2.Wolf wolf1 = new Problem2.Wolf("Predator", "Ghost");
            Problem2.Wolf wolf2 = new Problem2.Wolf("Predator", "Nymeria");
            Problem2.Wolf wolf3 = new Problem2.Wolf("Predator", "Grey Wind");

            Problem2.Fox fox1 = new Problem2.Fox("Predator", "Eevee");
            Problem2.Fox fox2 = new Problem2.Fox("Predator", "Vulpix");

            Problem2.Hare hare1 = new Problem2.Hare("Herbivorous", "Bugs Bunny");
            Problem2.Hare hare2 = new Problem2.Hare("Herbivorous", "Easter Bunny");

            Problem2.Deer deer1 = new Problem2.Deer("Herbivorous", "Bambi");
            Problem2.Deer deer2 = new Problem2.Deer("Herbivorous", "Faline");
            Problem2.Deer deer3 = new Problem2.Deer("Herbivorous", "Sven");
            Problem2.Deer deer4 = new Problem2.Deer("Herbivorous", "Mena");

            Problem2.Animal[] arrayOfAnimals = new Problem2.Animal[] {wolf1, wolf2, wolf3, fox1, fox2, deer1, deer2, deer3, deer4, hare1, hare2};




            for (int i = 0; i < arrayOfAnimals.Length; i++)
            {
                int counter = 0;
                bool check = false;

                Problem2.Herbivorous myHerbivorousAnimal = arrayOfAnimals[i];
                check = myHerbivorousAnimal is Problem2.Herbivorous;

                if (check == true)
                {
                    counter++;
                }
            }
        }
    }`

And I have a compilation issue :

Error CS0266 Cannot implicitly convert type 'Problem2.Animal' to 'Problem2.Herbivorous'. An explicit conversion exists (are you missing a cast?)

so what went wrong? or is there a right way to count my "Predators" and "Herbivorous" in my array of "Animals"? THX

Upvotes: 0

Views: 308

Answers (1)

adjan
adjan

Reputation: 13684

Problem2.Herbivorous myHerbivorousAnimal = arrayOfAnimals[i];

You are trying to cast an Animal into Herbivoros, but not every Animal is Herbivoros. You can just do the is check on the current array entry:

 check =  arrayOfAnimals[i] is Problem2.Herbivorous;

by the way, I would not use an extra bool but just use if

if (arrayOfAnimals[i] is Problem2.Herbivorous)
{
   counter++;
}

And you shout put you counter out of the for loop, else it wont count anything:

int counter = 0;
for (int i = 0; i < arrayOfAnimals.Length; i++)
{
    if (arrayOfAnimals[i] is Problem2.Herbivorous)
    {
       counter++;
    }
}

Upvotes: 1

Related Questions