471,354 Members | 1,191 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

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

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"
Jan 31 '06 #1
3 1329
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....

Jan 31 '06 #2
The minimum requirement to use foreach over an object is that it implements
the IEnumerable inteface
http://msdn.microsoft.com/library/de...mberstopic.asp
You can find some foreach info at
http://msdn.microsoft.com/library/de...hstatement.asp

Sayed Ibrahim Hashimi
www.sedodream.com
"Jeff User" wrote:
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"

Jan 31 '06 #3
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.
--

Stoitcho Goutsev (100)
"Sayed Ibrahim Hashimi" <Sa*****************@discussions.microsoft.com>
wrote in message news:CF**********************************@microsof t.com...
The minimum requirement to use foreach over an object is that it
implements
the IEnumerable inteface
http://msdn.microsoft.com/library/de...mberstopic.asp
You can find some foreach info at
http://msdn.microsoft.com/library/de...hstatement.asp

Sayed Ibrahim Hashimi
www.sedodream.com
"Jeff User" wrote:
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"

Jan 31 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Caleb Hattingh | last post: by
13 posts views Thread by jbraly | last post: by
2 posts views Thread by preport | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.