Error entering data to an interface type object c #

0

I have an exercise that I have to create an IAnimal interface and two classes that use that Dog and Cat , because in the interface I have to create get set named Name and a method that returns a string called SaySomething, the question is that in the exercise I have to create in the Main an object type IAnimal already Through a combination of loop and switch case I have to enter data to the two classes Dog and Cat but I get a compilation error. This is a part of my code from the Main:

       IAnimal []animal=new  IAnimal [3];
        char ch;
        int i = 3;
        while (i != 0)
        {

            Console.WriteLine("Its a dog or cat? (d/c)");
            ch = char.Parse( Console.ReadLine());
            Console.WriteLine("What its her name?");
            string name = Console.ReadLine();
            Console.WriteLine("How he does?");
            string voice = Console.ReadLine();
            switch (ch)
            {
                case 'd':

                         animal[i] = new Dog(name,voice);

                        break;

                case 'c':

                        animal[i] = new Cat(name,voice);
                        break;

                default:
                    Console.WriteLine("Default case");
                    continue;

            }
            i--;
        }
    
asked by Julio Mizrahi 27.04.2018 в 22:22
source

2 answers

2

mainly an interface is only a contract that a class must fulfill, therefore it can not be instantiated directly. Other classes can implement their properties and methods.

First, you must create your interface:

interface IAnimales
{
    string Name { get; set; }
    string SaySomething();
}

Next, create your Cat and Dog class by implementing that interface:

class Cat : IAnimales
{
    public string Name
    {
        get; set;
    }

    public string SaySomething()
    {
        return "miauuuu";
    }
}
class Dog: IAnimales
{
    public string Name
    {
        get; set;
    }

    public string SaySomething()
    {
        return "goauuu";
    }
}

Subsequently, in the Main method you do the instantiation, depending on the type of animal with your loop and switch, it would be something like this:

static void Main(string[] args)
    {
        for (int i = 0; i < 1; i++)
        {
            IAnimales objAnimal;
            switch (i)
            {
                case 0:
                    objAnimal = new Cat { Name ="Kitty"};
                    Console.WriteLine($"My {objAnimal.Name} say {objAnimal.SaySomething()}");
                    break;
                case 1:
                    objAnimal = new Cat { Name="Puppy"};
                    Console.WriteLine($"My {objAnimal.Name} say {objAnimal.SaySomething()}");
                    break;
                default:
                    break;
            }

        }
    }

I hope it serves you ...

    
answered by 01.05.2018 / 22:05
source
-1

This line is wrong:

IAnimal []animal=new  IAnimal [3];

should be:

IAnimal animal[] = new IAnimal[3];

or better yet:

var animal = new IAnimal[3];
    
answered by 29.04.2018 в 23:27