If I have a Person
class that outlines the properties of a person and one of People
that just contains a List<Person>
like so:
public class Person
{
public Person(){}
public int personID {get;set;}
public string name {get;set;}
public string address {get;set;}
}
public class People
{
public People(){}
public List<Person> peoples {get;set;}
public List<Person> getPeople()
{
List<Person> p = new List<Person>();
//create database connection
//open datareader
Person onePerson = new Person();
//write data to eahc property type and add data to list
p.Add(onePerson);
return p;
}
}
To call the database method that gets the people from the database I could do this:
static void main(string[] args)
{
People p = new People();
p.getPeople();
}
Is this how I would do this in terms of OOP?
I'm just beginning to use these techniques in my coding as usually I would remove the getPeople()
method from the People
class and do this in main()
:
static void main(string[] args)
{
List<Person> people = getPeople();
}
private static List<Person> getPeople()
{
List<Person> p = new List<Person>();
//create database connection
//open datareader
//load data into list
Person onePerson = new Person();
p.Add(onePerson);
return p;
}
peoples
is a rather confusing name for aList<Person>
, especially in a class calledPeople
. – Mat's Mug Nov 5 '13 at 1:47