ok so im trying to search list for a student by the last name and i keep getting a error as
Error 1 Cannot convert type 'string' to 'Project2.Person'
and i do not know how to fix it. any advice?

class StudentData
    {   private List<string> Page;
 
        public StudentData()
        {   Page = new List<string>();
        }

      

        public Person searchStudent(string Lname) //searches for a Person based on their last name
        {
            foreach(Person search in Page)
            {
                if (search.getLastName().ToLower() == Lname.ToLower())
                {
                    return search;
                }
            }
            r

You're saying this

foreach (Person search in Page)

Which suggests Page should be something equivalent to an IEnumerable of Person. (List<Person>, IEnumerable<Person>, etc.).

However, in your Page declaration and instantiation, you have this

private List<string> Page;
...
Page = new List<string>();

Hence the error. You have a list of strings based on your declaration & instantiation, but your loop is trying to get a Person.

o ok so how would i get it to work with what i have?

something like this i would imagine:

foreach (string person in page)
{
   if (person.getLastName().ToLower() == Lname.ToLower())
                {
                    return search;
                }
}

I may be getting this very wrong as im a total n00b....

but how are you differentiating between first and last name in your string list?

presumably you're just adding to the list the first/last name as one string?

so each list item is like:

"Dan Turner"
"ROT C89"
or whatever?

so you may want to split each string into parts...

foreach (string person in page)
{
       string [] firstLast = person.Split(' ');
   if (firstLast[1].ToLower() == Lname.ToLower())
                {
                    return person;
                }
}

either than or use

if (person.ToLower().indexOf(Lname.ToLower())>-1)

o ok so how would i get it to work with what i have?

Page needs to be a List<Person> and not a List<string> in order for you to execute your search method. If Page is going to be a List<string> then you will have to change the method. How is Page being populated? I've seen its declaration and instantiation, but I do not see its population.

Step back and look at your total code and make sure your code matches your intent.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.