Which interface(s) do I need to iterate over objects

J

Jeff User

Hi
I want to create a rather simple object with a few string attributes
to hold some database user information such as:

public class UserInfo
{
//Basic user information for each user associated with a database.
private string sDbName;
private string sUserName;
.... other stuff....

public string dbName
{
get {...} set{ }
}
public string UserName
{ ........etc.etc....
}
......more stuff
}

There can be many UserInfo objects for each database.

Then, I was thinking another object , say dbUsers, that contains many
UserInfo objects. One dbUsers object for each database. (Maybe now, I
would not need to keep the dbName in the UserInfo object as all
UserInfo objects would always belong to some dbUsers object.


And finally, an object, say AllDbsAndUsers to hold all dbUser
objects.

I would create one dbUser object at a time, populate it with several
UserInfo objects, for a particular db, and then "add" it to the
AllDbsAndUsers object.

To use AllDbsAndUsers I would like to be able to "add" objects, get
objects based on db name, etc...
It will be handy in several situations to be able to use foreach loops
to run through one object, while processing the inner objects.

I am looking at IEnumerable, IEnumerator, IDictionary, DictionaryBase
and Hashtables. I really am not sure where to begin.
Is there also an Interface called Iterator?

What is the basic minimum needed any any object to be able to use a
foreach loop?
Are the requirements different if I want to have an "add" method?

Thanks

Jeff
"I'll get this sooner or later"
 
S

Steven Nagy

Check out IList and also CollectionBase
You could always inherit from HashTable (my favourite)

There's a good example in MSDN help files that demonstrates all this.
Can't remember what its called though.
I think it used the "Widget" class....
 
S

Stoitcho Goutsev \(100\)

Just a little correction.

The minimum requirements is the object to expose public GetEnumerator method
that returns object exposing all the methods in IEnumerator.
Implementing IEnumerable or IEnumerator is not a must.

However implementing these interfaces is a good idea beacuse it makes type's
documentation clearer and easier to understand. Not implementing the
interfaces on the other hand can be used to improve the performance in a
case of collection of value types.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top