Home > Software design >  Trying to add class objects to a list, but getting "no overload for method add takes 3 argument
Trying to add class objects to a list, but getting "no overload for method add takes 3 argument

Time:09-13

I'm trying to solve this problem:

Description of the problem as an image.

I am working on a homework exercise that is printing info about citizens and I am stuck on the part where I have to add the citizens to a list of the class Citizen and getting an error when adding the created objects to a list I would want to print in the console. This is my code:

class Program
{
    static void Main(string[] args)
    {
        List<Citizen> citizens = new List<Citizen>();

        string name = Console.ReadLine();
        string country = Console.ReadLine();
        int age = int.Parse(Console.ReadLine());
        citizens.Add(name,age,country);

        string end = Console.ReadLine().ToUpper();
        while (end!="End")
        {
            name = Console.ReadLine();
            country = Console.ReadLine();
            age = int.Parse(Console.ReadLine());

            citizens.Add(name, age, country);
        }
    }
}

public interface IResident
{
    public string Name { get;}
    public string Country { get;}

    public string GetName();
}

public interface IPerson
{
    public string Name { get;}
    public int Age { get;}

    public string GetName();
}

public class Citizen : IPerson, IResident
{
    public Citizen(string name, int age, string country)
    {
       this.Name = name;
       this.Age = age;
       this.Country = country;
    }

    public string Name { get; private set; }
    public int Age { get; private set; }
    public string Country { get; private set; }

    string IPerson.GetName()
    {
        return $"{this.Name}";
    }
    string IResident.GetName()
    {
        return "Mr/Ms/Mrs ";
    }
}

CodePudding user response:

You need to construct a Citizen object and then add that to your list, as citizens is of type List<Citizen>

So in your code wherever you attempt to add to your list, it must become citizens.Add(new Citizen(name,age,country));

The reason for the error message is clear, as there truly is no .Add() overload that takes 3 parameters, but that isn't the problem really. Even if there was such an overload, you would still get an error due to type mismatch. List<T> will strictly enforce whatever T you declare for it

CodePudding user response:

You have declared your list to contain citizens, so you need to add a Citizen to your list. However, your code is currently trying to add a name, age and country to the list, which are strings and integers.

You must therefore create a new Citizen and add it to the list. You can do this by new Citizen(name, age, country);

So the correct way to implement the add function would be: citizens.Add(new Citizen(name,age,country));

As a related extra, currently, you are calling Add in two places - it may be advisable to only do this in one place to keep things simple. Good luck!

  • Related