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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff User

    #1

    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"
  • Steven Nagy

    #2
    Re: Which interface(s) do I need to iterate over objects

    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....

    Comment

    • Sayed Ibrahim Hashimi

      #3
      RE: Which interface(s) do I need to iterate over objects

      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
      John Doe is a professional web developer. He is blogging about ASP.NET and web developing.

      "Jeff User" wrote:
      [color=blue]
      > 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"
      >[/color]

      Comment

      • Stoitcho Goutsev \(100\)

        #4
        Re: Which interface(s) do I need to iterate over objects

        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" <SayedIbrahimHa shimi@discussio ns.microsoft.co m>
        wrote in message news:CF2664AF-8A45-4ED2-B869-6B037F15088F@mi crosoft.com...[color=blue]
        > 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:
        >[color=green]
        >> 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"
        >>[/color][/color]


        Comment

        Working...