Help in creating a collection class

  • Thread starter Thread starter Matthew Hood
  • Start date Start date
M

Matthew Hood

Here's the situation.
I am developing an ASP.NET web application. Most of my forms will be
accessing a database (MS Access) for either record
creation/deletion/updating or for list lookups. The problem I have is my
boss constantly wants to revise table and field names and I am the one that
has to make sure everthing works after the fact. What I want to do is
create 2 classes.

The first one is my "Record" wrapper class that will map to the table field
names. This is to provide me a consistant interface so when the database
field name is changed, all I have to do is change the field mapping in this
class.

The second class is my "Records" collection. I can create the database
methods myself, but I need to be able to set the rest of the interface up
and this is where I get a little confused. There are several different
collection objects available: array, arraylist, collection, dictionary...
I'm not sure what the best method is and how to go about implementing it.
What are your recomendations for this? I was able to do this easily in VB6,
but I'm not sure how to do it in .NET

1: I need to be able to use For Each Next... syntax
2: I need to be able to use either the Collection(Index as Integer) or
Collection(KeyName as String) methods to access an item.
3: I need to be able to strict types. Collection.Add(CollectionItem as
MyType)
4: Sorting is not needed. I will accomplish this in my SQL string.
4: Performance is an issue, but ease of use and functionality is a higher
priority.

Can anybody help explain what I need to do, and/or point me to some good
examples on the web where I can learn how to do it?

TIA,
-Matt
 
In your case, you can simply implement IEnumerable on your class, which will
alow you to use For Each.
You can use add index methods on your class to support 2.
Just add Add((CollectionItem as MyType) method on your class, you will have
all you need.
You could also implement System.Collections.IList on your class, but this
interface contains many methods you might not want to implement.
The advantage of implement IList is that you can use your collection on many
framework methods, which might not be important for you.

You can look at the implementation of StringCollection class in .Net
Framework (download rotor source and go to
fx\src\compmod\system\collections\specialized.)
Or down a strong-typed collection sample from
http://www.gotdotnet.com/Community/...mpleGuid=758a20b8-15c4-4195-bcc6-d9d8d7951ddc

Hope this helps,
Gang Peng
[MS]
 
Back
Top