Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Enumerator

Enumerator

Scheduled Pinned Locked Moved C#
salesregex
7 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CodingYoshi
    wrote on last edited by
    #1

    I have a class which has a dictionary and I don't want to reveal this information to the outside. So I simply made it a private member. I want people to enumerate through my class so I did the following: public class CustomerCollection : IEnumerable<KeyValuePair<string, Customer>> { #region IEnumerable<KeyValuePair<string,Store>> Members public IEnumerator<KeyValuePair<string, Customer>> GetEnumerator() {       return this._districts.GetEnumerator(); } #endregion #region IEnumerable Members IEnumerator IEnumerable.GetEnumerator() {       return this._districts.GetEnumerator(); } #endregion } This works fine but during enumeration, the client has to write code as below: foreach (KeyValuePair<string, Customer> c in this._customers) // _customers is an instance of CustomerCollection { c.Value.Name = "Whatever"; } What should I do if I want the client to be able to do this: foreach (Customer c in this._customers) { c.Name = "whatever"; }

    CodingYoshi Artificial Intelligence is no match for Human Stupidity.

    A D A N 4 Replies Last reply
    0
    • C CodingYoshi

      I have a class which has a dictionary and I don't want to reveal this information to the outside. So I simply made it a private member. I want people to enumerate through my class so I did the following: public class CustomerCollection : IEnumerable<KeyValuePair<string, Customer>> { #region IEnumerable<KeyValuePair<string,Store>> Members public IEnumerator<KeyValuePair<string, Customer>> GetEnumerator() {       return this._districts.GetEnumerator(); } #endregion #region IEnumerable Members IEnumerator IEnumerable.GetEnumerator() {       return this._districts.GetEnumerator(); } #endregion } This works fine but during enumeration, the client has to write code as below: foreach (KeyValuePair<string, Customer> c in this._customers) // _customers is an instance of CustomerCollection { c.Value.Name = "Whatever"; } What should I do if I want the client to be able to do this: foreach (Customer c in this._customers) { c.Name = "whatever"; }

      CodingYoshi Artificial Intelligence is no match for Human Stupidity.

      A Offline
      A Offline
      Adam R Harris
      wrote on last edited by
      #2

      Have CustomerCollection contain a list of Customer objects So your code should look something like this. NOTE: this is not tested and probably wont compile

      public class Customer
      {
          public string Name{get; set;}
          ...
      }
      
      public class CustomerCollection : CollectionBase
      {
          public Customer this[int index]{
              get{ return innerList[index];}
              set{ innerList[index] = value;}
          }
      
          public void Add(Customer item){
             innerList.Add(item);
          }
      }
      

      If you inherit CollectionBase (System.Collections.Specialized i think) you don't to inherit or even implement IEnumerable because CollectionBase does all the dirty work for you.

      If at first you don't succeed ... post it on The Code Project and Pray.

      C 1 Reply Last reply
      0
      • C CodingYoshi

        I have a class which has a dictionary and I don't want to reveal this information to the outside. So I simply made it a private member. I want people to enumerate through my class so I did the following: public class CustomerCollection : IEnumerable<KeyValuePair<string, Customer>> { #region IEnumerable<KeyValuePair<string,Store>> Members public IEnumerator<KeyValuePair<string, Customer>> GetEnumerator() {       return this._districts.GetEnumerator(); } #endregion #region IEnumerable Members IEnumerator IEnumerable.GetEnumerator() {       return this._districts.GetEnumerator(); } #endregion } This works fine but during enumeration, the client has to write code as below: foreach (KeyValuePair<string, Customer> c in this._customers) // _customers is an instance of CustomerCollection { c.Value.Name = "Whatever"; } What should I do if I want the client to be able to do this: foreach (Customer c in this._customers) { c.Name = "whatever"; }

        CodingYoshi Artificial Intelligence is no match for Human Stupidity.

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        I've never tried it, but it might work if you can provide an implicit conversion operator from Customer to KeyValuePair<string, Customer> although intellisense will still show as wanting a KeyValuePair so it wouldn't be intuitive. You may need to wrap the dictionary a little more to provide a different enumerator.

        Dave
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
        Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
        Why are you using VB6? Do you hate yourself? (Christian Graus)

        1 Reply Last reply
        0
        • C CodingYoshi

          I have a class which has a dictionary and I don't want to reveal this information to the outside. So I simply made it a private member. I want people to enumerate through my class so I did the following: public class CustomerCollection : IEnumerable<KeyValuePair<string, Customer>> { #region IEnumerable<KeyValuePair<string,Store>> Members public IEnumerator<KeyValuePair<string, Customer>> GetEnumerator() {       return this._districts.GetEnumerator(); } #endregion #region IEnumerable Members IEnumerator IEnumerable.GetEnumerator() {       return this._districts.GetEnumerator(); } #endregion } This works fine but during enumeration, the client has to write code as below: foreach (KeyValuePair<string, Customer> c in this._customers) // _customers is an instance of CustomerCollection { c.Value.Name = "Whatever"; } What should I do if I want the client to be able to do this: foreach (Customer c in this._customers) { c.Name = "whatever"; }

          CodingYoshi Artificial Intelligence is no match for Human Stupidity.

          A Offline
          A Offline
          Alan N
          wrote on last edited by
          #4

          Hi, If you expose the Dictionary's value collection you will get this functionality.

          Dictionary<string, Customer> dic;
          ICollection<Customer> Customers {
            get { return this.dic.Values; }
          }
          

          I think that's all you need as the value collection is enumerable. Alan.

          1 Reply Last reply
          0
          • C CodingYoshi

            I have a class which has a dictionary and I don't want to reveal this information to the outside. So I simply made it a private member. I want people to enumerate through my class so I did the following: public class CustomerCollection : IEnumerable<KeyValuePair<string, Customer>> { #region IEnumerable<KeyValuePair<string,Store>> Members public IEnumerator<KeyValuePair<string, Customer>> GetEnumerator() {       return this._districts.GetEnumerator(); } #endregion #region IEnumerable Members IEnumerator IEnumerable.GetEnumerator() {       return this._districts.GetEnumerator(); } #endregion } This works fine but during enumeration, the client has to write code as below: foreach (KeyValuePair<string, Customer> c in this._customers) // _customers is an instance of CustomerCollection { c.Value.Name = "Whatever"; } What should I do if I want the client to be able to do this: foreach (Customer c in this._customers) { c.Name = "whatever"; }

            CodingYoshi Artificial Intelligence is no match for Human Stupidity.

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            CodingYoshi wrote:

            What should I do if I want the client to be able to do this: foreach (Customer c in this._customers) { c.Name = "whatever"; }

            How about this?

            public class CustomerCollection : IEnumerable<Customer>
            {
            public IEnumerator<Customer> GetEnumerator()
            {
            foreach(KeyValuePair<string,Customer> item in _districts)
            yield return item.Value;
            }

            System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
            

            }

            :)

            Navaneeth How to use google | Ask smart questions

            C 1 Reply Last reply
            0
            • A Adam R Harris

              Have CustomerCollection contain a list of Customer objects So your code should look something like this. NOTE: this is not tested and probably wont compile

              public class Customer
              {
                  public string Name{get; set;}
                  ...
              }
              
              public class CustomerCollection : CollectionBase
              {
                  public Customer this[int index]{
                      get{ return innerList[index];}
                      set{ innerList[index] = value;}
                  }
              
                  public void Add(Customer item){
                     innerList.Add(item);
                  }
              }
              

              If you inherit CollectionBase (System.Collections.Specialized i think) you don't to inherit or even implement IEnumerable because CollectionBase does all the dirty work for you.

              If at first you don't succeed ... post it on The Code Project and Pray.

              C Offline
              C Offline
              CodingYoshi
              wrote on last edited by
              #6

              I don't want to inherit because I want to hide the interface of the inner collection. Also, I want this to be a readonly collection.

              CodingYoshi Artificial Intelligence is no match for Human Stupidity.

              1 Reply Last reply
              0
              • N N a v a n e e t h

                CodingYoshi wrote:

                What should I do if I want the client to be able to do this: foreach (Customer c in this._customers) { c.Name = "whatever"; }

                How about this?

                public class CustomerCollection : IEnumerable<Customer>
                {
                public IEnumerator<Customer> GetEnumerator()
                {
                foreach(KeyValuePair<string,Customer> item in _districts)
                yield return item.Value;
                }

                System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
                {
                    return GetEnumerator();
                }
                

                }

                :)

                Navaneeth How to use google | Ask smart questions

                C Offline
                C Offline
                CodingYoshi
                wrote on last edited by
                #7

                Thanks! That is exactly what I am looking for. I will give that a try and let you know if it works; although, it looks as it should.

                CodingYoshi Artificial Intelligence is no match for Human Stupidity.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups