Improving the StudentDataAccess Class for ASP.NET 2.0 - Creating the StudentData AccessCollection class
(Page 2 of 5 )
In the next section we are going to create a method called GetAllStudents() which returns all the rows of the Students table as objects of type StudentDataAccess in a collection object, which we will create in this section, of type StudentDataAccessCollection. The method signature looks like this
public static StudentDataAccessCollection GetAllStudents()
Let's create the StudentDataAccessCollection class. Right click on the App_Code folder, click on Add new Item menu option, select a class template, name it StudentDataAccessCollection.cs and click on OK. Replace the auto-generated code of the class file with the following code.
using System;
using System.Collections;
public class StudentDataAccessCollection{
private ArrayList list;
public ArrayList List{
get { return list; }
}
public StudentDataAccessCollection(){
this.list = new ArrayList();
}
public void Add(StudentDataAccess student){
this.list.Add(student);
}
public void Remove(StudentDataAccess student){
this.list.Remove(student);
}
public int Count{
get { return this.list.Count; }
}
public StudentDataAccess this[int index]{
get { return (StudentDataAccess)this.list[index]; }
}
public IEnumerator GetEnumerator(){
return list.GetEnumerator();
}
}
You might think that we can write the signature of the GetAllStudents() method to return a .NET Collection object as follows, instead of creating our own collection class.
public static ArrayList GetAllStudents()
Yes, you can do that, but the problem is that the ArrayList.Add() method accepts the general object data type as a parameter, so it can contain any data type. We need a way to have a collection object with an Add() method that accepts the data type we are working with. To make this possible we have defined a private ArrayList that will be used for storing our objects.
We have defined the ArrayList object as private and created methods, like Add(), as public with the data type we want to work with, in this case the StudentDataAccess type, as the parameter. Inside those public methods we pass the StudentDataAccess object as a value to the ArrayList.Add() method. Using this technique we guarantee that the StudentDataAccessCollection.Add() method will add only objects of type StudentDataAccess to the inner private ArrayList object. Also using this technique you can write code that is strongly typed, as we are going to see in the testing page.
The same principle applies to the Remove() method, the property Count and the indexer. We simply choose what we want to provide as part of our collection's functionality by implementing public methods and properties, that use the private ArrayList methods and properties, for storing and retrieving our strongly typed objects.
To use a foreach statement with a StudentDataAccessCollection object you must provide a public method with the following signature:
public IEnumerator GetEnumerator()
It's used by the foreach statement to iterate over the objects of the collection. we simply called the ArrayList.GetEnumerator() to return the enumerator for us. We didn't do anything new other than use the private ArrayList object's methods and properties. Create a new class file in the App_Code folder, name it StudentDataAccessCollection.cs and replace the class template code with the above class definition.
Let's write the GetAllStudents() method and test it.
Next: Creating the GetAllStudents() method >>
More ASP.NET Articles
More By Michael Youssef