开发者

Passing in data-sets into classes or methods?

I have a class that performs some operations on a set of data. The set of data to be processed is derived from a SQL query and usually consists of a few rows with 5 to 10 fields in the row. An example would be ..

"Bob", "Smith", "57", "555-555-5555", "Nursing"
"Pam", "Watson", "32", "555-555-3494", "Pre-Law"
"Sam", "Johnson", "42", "555-555-9382", "History"
"Paul", "Jenkins", "40", "555-555-3720", "Geography"

What is the best method to pass this data into a class? Or for that matter how about a method?

I want to make the class as generic as possible so I want to pass the data into it rather than have the class perform the data access necessary to get the data. Within the class I will need to be able to iterate through the data in order to perform some operation on it

I was initially thinking of passing in a dictionary object or a multi-dimensional array. I am looking to build the application in .NET. Thanks 开发者_开发知识库a bunch.


First, you should create a class to represent each row of the data; in this case Student looks like a good fit:

class Student
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public string Telephone { get; set; }
    public string Major { get; set; }
}

You should create and initialize one Student class for each row of data. You can then add them to a collection class. A HashSet<Student> might be an appropriate choice:

ISet<Student> students = new HashSet<Student>();
for each row in query result  // (pseudocode)
{ 
    students.Add(new Student { FirstName = queryResult[0], 
                        LastName = queryResult[1] }); // other fields omitted
}

I haven't gone into exact detail because I'm not sure how you are accessing the database.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜