0

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.

webworm
  • 10,587
  • 33
  • 120
  • 217

1 Answers1

1

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.

Justin
  • 6,611
  • 3
  • 36
  • 57
  • Thanks Justin! That looks like just what I was looking for. It may be off topic but what does `ISet` Stand for? – webworm Feb 25 '11 at 18:51
  • 1
    The `I` means it is an *[interface](http://msdn.microsoft.com/en-us/library/87d83y5b%28v=VS.100%29.aspx)*, and a *[set](http://en.wikipedia.org/wiki/Set_%28computer_science%29)* is a collection of unique elements. You could make the type of `students` be `HashSet` instead, but using `ISet` makes it easier to change implementations should you ever want to. See [this question](http://stackoverflow.com/questions/400135/c-listt-or-ilistt) for more explanation of the pattern. – Justin Feb 25 '11 at 19:00