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
  1. Home
  2. General Programming
  3. C#
  4. Giving an object multiple enumerator types

Giving an object multiple enumerator types

Scheduled Pinned Locked Moved C#
help
8 Posts 3 Posters 0 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.
  • R Offline
    R Offline
    Rareed
    wrote on last edited by
    #1

    Hi everyone, I've been writing a class that I want to be able to enumerate over different types in a foreach statement. I've tried googling with no luck, and I was hoping that someone here could help me out.

    C 1 Reply Last reply
    0
    • R Rareed

      Hi everyone, I've been writing a class that I want to be able to enumerate over different types in a foreach statement. I've tried googling with no luck, and I was hoping that someone here could help me out.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      You can write enumerators for different types, within the class, so that the class is able to be enumerated over using different types, via different properties. Is that what you want ?

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      R 1 Reply Last reply
      0
      • C Christian Graus

        You can write enumerators for different types, within the class, so that the class is able to be enumerated over using different types, via different properties. Is that what you want ?

        Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

        R Offline
        R Offline
        Rareed
        wrote on last edited by
        #3

        I think so. Do you know of an example anywhere that you could point me to? Right now, I have something like: IndexType IEnumerator.Current{...} KeyValuePair IEnumerator>.Current{...} and I am getting the error Error 1 foreach statement cannot operate on variables of type 'Myclass' because it implements multiple instantiations of 'System.Collections.Generic.IEnumerable', try casting to a specific interface instantiation 232 The foreach statement is foreach (int i in info) where info is of type Myclass

        C R 2 Replies Last reply
        0
        • R Rareed

          I think so. Do you know of an example anywhere that you could point me to? Right now, I have something like: IndexType IEnumerator.Current{...} KeyValuePair IEnumerator>.Current{...} and I am getting the error Error 1 foreach statement cannot operate on variables of type 'Myclass' because it implements multiple instantiations of 'System.Collections.Generic.IEnumerable', try casting to a specific interface instantiation 232 The foreach statement is foreach (int i in info) where info is of type Myclass

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          I'd expect you need a property that returns the enumerator, so you use the property to select which enumerator to use.

          Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

          R 1 Reply Last reply
          0
          • C Christian Graus

            I'd expect you need a property that returns the enumerator, so you use the property to select which enumerator to use.

            Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            R Offline
            R Offline
            Rareed
            wrote on last edited by
            #5

            First off, thank you very much for the help so far. I am a little unclear on what exactly you mean here. Could you please be more specific what property I need to implement to make this work with a foreach statement? I've been looking and can't seem to find any documentation on it. Everything I've seen seems to assume that you only want to iterate for one data type. Is what I'm trying to do even possible?

            C 1 Reply Last reply
            0
            • R Rareed

              First off, thank you very much for the help so far. I am a little unclear on what exactly you mean here. Could you please be more specific what property I need to implement to make this work with a foreach statement? I've been looking and can't seem to find any documentation on it. Everything I've seen seems to assume that you only want to iterate for one data type. Is what I'm trying to do even possible?

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              I thought you could create properties which return an IEnumerable, and then those would allow a single class to specify several types which could be iterated over. So you end up with foreach(int n in myClass.Ints) and foreach(string s in myClass.Strings) you can't have more than one type for foreach(string s in myClass) Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              R 1 Reply Last reply
              0
              • C Christian Graus

                I thought you could create properties which return an IEnumerable, and then those would allow a single class to specify several types which could be iterated over. So you end up with foreach(int n in myClass.Ints) and foreach(string s in myClass.Strings) you can't have more than one type for foreach(string s in myClass) Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                R Offline
                R Offline
                Rareed
                wrote on last edited by
                #7

                Ok, I see what you're saying now. Thank you very much for your help.

                1 Reply Last reply
                0
                • R Rareed

                  I think so. Do you know of an example anywhere that you could point me to? Right now, I have something like: IndexType IEnumerator.Current{...} KeyValuePair IEnumerator>.Current{...} and I am getting the error Error 1 foreach statement cannot operate on variables of type 'Myclass' because it implements multiple instantiations of 'System.Collections.Generic.IEnumerable', try casting to a specific interface instantiation 232 The foreach statement is foreach (int i in info) where info is of type Myclass

                  R Offline
                  R Offline
                  Robert Rohde
                  wrote on last edited by
                  #8

                  Hi, with the given error message I assume you have a class like this:

                  class SampleClass : IEnumerable<int>, IEnumerable<string> { ... }

                  If you want to iterate over the ints then do:

                  SampleClass mySampleInstance;
                  foreach (int i in (IEnumerable<int> )mySampleInstance) { ... }

                  or if you want to iterate over the strings:

                  foreach (string s in (IEnumerable<string> )mySampleInstance) { ... }

                  Robert

                  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