Reputation: 5
Well I have a class Person:
public class Person
{
public string firstName { get; set; }
public string lastName { get; set; }
public int age { get; set; }
public IEnumerable<Person> GetPersons(string path)
{
var personlist = new List<Person>();
var lines = System.IO.File.ReadAllLines(path);
foreach (var line in lines)
{
var split = line.Split(";");
var firstName = split[0];
var lastName = split[1];
var age = split[2];
Person person = new Person
{
firstName = firstName,
lastName = lastName,
age = int.Parse(age),
};
personlist.Add(person);
}
return personlist;
}
}
And in my Main method I'd like to asign the method GetPersons(string path)
to a list
var list = new List<Person>();
list = GetPersons("persons.csv");
I does not compile with error CS0103: The name 'GetPersons' does not exist in the current context
Upvotes: 0
Views: 321
Reputation: 136174
You've made GetPersons
an instance method which means you'd need an instance of a Person
to call it - this doesnt quite make sense for what you're doing.
There is some sense in making it static
:
public class Person
{
public static IEnumerable<Person> GetPersons(string path)
{
// ... //
}
}
Then call it as:
var list = Person.GetPersons("persons.csv");
But you should consider if the Person
class is the correct place for this code.
Upvotes: 1
Reputation: 14389
you need to create a new instance of Person class and then call GetPersons. Try like:
var list = new List<Person>();
var person = new Person();
list = person.GetPersons("persons.csv");
Upvotes: 0