0

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
3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by apegram
0

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.

Edited by apegram: n/a

0

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

-1

something like this i would imagine:

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

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)

Edited by danturn: n/a

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.