Home > Back-end >  Trying to make a list that starts empty but can be added to in console command error CS1061: 'L
Trying to make a list that starts empty but can be added to in console command error CS1061: 'L

Time:12-12

So i am making a program with multiple menues and one of the menus is enter a persons name into a regestry i did most of it i think but i get the error error CS1061: 'List' does not contain a definition for 'nameimput' and no accessible extension method 'nameimput' accepting a first argument of type 'List'

heres the code

List<PeopleName> PeopleList = new List<PeopleName>();

WriteLine("People in the registry");
WriteLine("---------");

            
foreach (var NameImput in PeopleList)
{
   if (PeopleList.Count == 0) 
   {
      WriteLine("No persons on registry");
      WriteLine("Please enter name >> ");
      nameImput = Convert.ToString(ReadLine());
      PeopleList.Add(new PeopleName(nameImput));

      if (1==1)
      {
        WriteLine("People in the Regristy");
        WriteLine("---------");
        WriteLine("{0}", PeopleList.nameimput);
        WriteLine();
        WriteLine("Enter a new name to be added >> ");
        nameImput = ReadLine();
        PeopleList.Add(new PeopleName(nameImput));
        }       
    } 
}

and

class PeopleName
{
    public string NameImput { get; set; }

    public PeopleName(string nameimput)
    {
        this.NameImput = nameimput;
    }
}

i dont see what the definition i didnt write is

CodePudding user response:

It looks like you're trying to access the nameImput property of the PeopleList list, but PeopleList is a list of PeopleName

CodePudding user response:

Looks like you are assigning a string input to an object of PeopleName. try something like this

foreach(var nameImput in PeopleList)
{
    nameImput.NameImput = Convert.ToString(Readline()) // now you setting the property which is a string to the input
    
}

That should solve the error

CodePudding user response:

You have this line:

WriteLine("{0}", PeopleList.nameimput);

I suspect that what you want is this:

WriteLine("{0}", PeopleList.LastOrDefault()?.NameImput);

(Assuming that you have System.Linq among your using statements.)

Incidentally, and just for the record: If by "NameImput" you mean "the name the user entered", you should spell it "NameInput".

  •  Tags:  
  • c#
  • Related