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. Enumerate Custom Class

Enumerate Custom Class

Scheduled Pinned Locked Moved C#
helpquestion
10 Posts 5 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.
  • D Offline
    D Offline
    Dowse
    wrote on last edited by
    #1

    Hi all Have started writing a Class to help me with a problem in my current project and I would like to be able enumerate the variables in the Class by using a 'foreach(object o in MyClass)' in my main program. I was getting a 'MyClass does not contain a public definition for GetEnumerator' but have worked out why and I am at the point where I could really do with some help with the GetEnumerator function.

    public class MyClass : IEnumerable
    {
    public System.DateTime datStart; //Schedule start date
    public System.DateTime datEnd; //Date of final occurrence (#1/1/1900# if N/A)
    public int intParam1; //Various Uses
    public int intParam2; //Various Uses
    //Other fields remove for clarity

    //Constructor
    public MyClass()
    {
        //Set defaults for new MyClass()
        datStart = new DateTime(1900, 1, 1);
        datEnd = datStart;
        intParam1 = -1;
        intParam2 = -1;
    }
    
    IEnumerator IEnumerable.GetEnumerator()
    {
        //What goes here?
    
        return null;  //Only added this line to stop compiler error
    }
    

    }

    I know there are many, (many, many,) references to this on the web but I haven't been able to re-work any of them to solve my specific problem. Any help would be MUCH appreciated. Thank you.

    T L D L 4 Replies Last reply
    0
    • D Dowse

      Hi all Have started writing a Class to help me with a problem in my current project and I would like to be able enumerate the variables in the Class by using a 'foreach(object o in MyClass)' in my main program. I was getting a 'MyClass does not contain a public definition for GetEnumerator' but have worked out why and I am at the point where I could really do with some help with the GetEnumerator function.

      public class MyClass : IEnumerable
      {
      public System.DateTime datStart; //Schedule start date
      public System.DateTime datEnd; //Date of final occurrence (#1/1/1900# if N/A)
      public int intParam1; //Various Uses
      public int intParam2; //Various Uses
      //Other fields remove for clarity

      //Constructor
      public MyClass()
      {
          //Set defaults for new MyClass()
          datStart = new DateTime(1900, 1, 1);
          datEnd = datStart;
          intParam1 = -1;
          intParam2 = -1;
      }
      
      IEnumerator IEnumerable.GetEnumerator()
      {
          //What goes here?
      
          return null;  //Only added this line to stop compiler error
      }
      

      }

      I know there are many, (many, many,) references to this on the web but I haven't been able to re-work any of them to solve my specific problem. Any help would be MUCH appreciated. Thank you.

      T Offline
      T Offline
      Thomas Weller 0
      wrote on last edited by
      #2

      You seem to misunderstand the concept of enumerators: You don't enumerate a single class, but you enumerate over collections like e.g. List or Dictionary. Not quite sure what you are trying to do, but IEnumerable is definitely not the way... Regards Thomas

      www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
      Programmer - an organism that turns coffee into software.

      D 1 Reply Last reply
      0
      • T Thomas Weller 0

        You seem to misunderstand the concept of enumerators: You don't enumerate a single class, but you enumerate over collections like e.g. List or Dictionary. Not quite sure what you are trying to do, but IEnumerable is definitely not the way... Regards Thomas

        www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
        Programmer - an organism that turns coffee into software.

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

        Thomas Thank you for taking the time to reply to my post. I am trying to implement foreach(object o in MyClass) for reporting and (possibly) serialization.

        T 1 Reply Last reply
        0
        • D Dowse

          Hi all Have started writing a Class to help me with a problem in my current project and I would like to be able enumerate the variables in the Class by using a 'foreach(object o in MyClass)' in my main program. I was getting a 'MyClass does not contain a public definition for GetEnumerator' but have worked out why and I am at the point where I could really do with some help with the GetEnumerator function.

          public class MyClass : IEnumerable
          {
          public System.DateTime datStart; //Schedule start date
          public System.DateTime datEnd; //Date of final occurrence (#1/1/1900# if N/A)
          public int intParam1; //Various Uses
          public int intParam2; //Various Uses
          //Other fields remove for clarity

          //Constructor
          public MyClass()
          {
              //Set defaults for new MyClass()
              datStart = new DateTime(1900, 1, 1);
              datEnd = datStart;
              intParam1 = -1;
              intParam2 = -1;
          }
          
          IEnumerator IEnumerable.GetEnumerator()
          {
              //What goes here?
          
              return null;  //Only added this line to stop compiler error
          }
          

          }

          I know there are many, (many, many,) references to this on the web but I haven't been able to re-work any of them to solve my specific problem. Any help would be MUCH appreciated. Thank you.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, As Thomas said enumerators is not what you need here. You need Reflection, which can give you access to all the data fields, constructors, methods, properties of your class. This is an example listing all the known colors in the SystemColors class:

          Assembly asm=Assembly.GetAssembly(Color.White.GetType());
          Type type=asm.GetType("System.Drawing.SystemColors", true);
          PropertyInfo\[\] pis=type.GetProperties();
          int n=pis.Length;
          log("There are "+n+" properties");
          foreach (PropertyInfo pi in pis) {
          	Color c=(Color)pi.GetValue(null, null);	// get the SystemColor
          	log(pi.Name+" = "+c.ToString());
          }
          

          BTW: you can only get information on class members, not on local variables inside some method. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


          D 2 Replies Last reply
          0
          • D Dowse

            Hi all Have started writing a Class to help me with a problem in my current project and I would like to be able enumerate the variables in the Class by using a 'foreach(object o in MyClass)' in my main program. I was getting a 'MyClass does not contain a public definition for GetEnumerator' but have worked out why and I am at the point where I could really do with some help with the GetEnumerator function.

            public class MyClass : IEnumerable
            {
            public System.DateTime datStart; //Schedule start date
            public System.DateTime datEnd; //Date of final occurrence (#1/1/1900# if N/A)
            public int intParam1; //Various Uses
            public int intParam2; //Various Uses
            //Other fields remove for clarity

            //Constructor
            public MyClass()
            {
                //Set defaults for new MyClass()
                datStart = new DateTime(1900, 1, 1);
                datEnd = datStart;
                intParam1 = -1;
                intParam2 = -1;
            }
            
            IEnumerator IEnumerable.GetEnumerator()
            {
                //What goes here?
            
                return null;  //Only added this line to stop compiler error
            }
            

            }

            I know there are many, (many, many,) references to this on the web but I haven't been able to re-work any of them to solve my specific problem. Any help would be MUCH appreciated. Thank you.

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

            A quick google found this[^] which may be along the lines of what you're looking for?

            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
            • L Luc Pattyn

              Hi, As Thomas said enumerators is not what you need here. You need Reflection, which can give you access to all the data fields, constructors, methods, properties of your class. This is an example listing all the known colors in the SystemColors class:

              Assembly asm=Assembly.GetAssembly(Color.White.GetType());
              Type type=asm.GetType("System.Drawing.SystemColors", true);
              PropertyInfo\[\] pis=type.GetProperties();
              int n=pis.Length;
              log("There are "+n+" properties");
              foreach (PropertyInfo pi in pis) {
              	Color c=(Color)pi.GetValue(null, null);	// get the SystemColor
              	log(pi.Name+" = "+c.ToString());
              }
              

              BTW: you can only get information on class members, not on local variables inside some method. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


              D Offline
              D Offline
              Dowse
              wrote on last edited by
              #6

              Thank to all that responded to my post. Luc, I think I can re-work your example to give me what I was after. Thank you.

              1 Reply Last reply
              0
              • D Dowse

                Thomas Thank you for taking the time to reply to my post. I am trying to implement foreach(object o in MyClass) for reporting and (possibly) serialization.

                T Offline
                T Offline
                Thomas Weller 0
                wrote on last edited by
                #7

                You could use reflection to enumerate over all the desired members of a class - like described in the post below. Alternatively simply implement a method object[] GetAllObjects() that returns everything you need. For serialization use the Serializable attribute and eventually the ISerializable interface (see here^ and here^). Regards Thomas

                www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
                Programmer - an organism that turns coffee into software.

                D 1 Reply Last reply
                0
                • T Thomas Weller 0

                  You could use reflection to enumerate over all the desired members of a class - like described in the post below. Alternatively simply implement a method object[] GetAllObjects() that returns everything you need. For serialization use the Serializable attribute and eventually the ISerializable interface (see here^ and here^). Regards Thomas

                  www.thomas-weller.de Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
                  Programmer - an organism that turns coffee into software.

                  D Offline
                  D Offline
                  Dowse
                  wrote on last edited by
                  #8

                  Thomas I will read up on reflection. It seems to be what I was after. Thank you.

                  1 Reply Last reply
                  0
                  • D Dowse

                    Hi all Have started writing a Class to help me with a problem in my current project and I would like to be able enumerate the variables in the Class by using a 'foreach(object o in MyClass)' in my main program. I was getting a 'MyClass does not contain a public definition for GetEnumerator' but have worked out why and I am at the point where I could really do with some help with the GetEnumerator function.

                    public class MyClass : IEnumerable
                    {
                    public System.DateTime datStart; //Schedule start date
                    public System.DateTime datEnd; //Date of final occurrence (#1/1/1900# if N/A)
                    public int intParam1; //Various Uses
                    public int intParam2; //Various Uses
                    //Other fields remove for clarity

                    //Constructor
                    public MyClass()
                    {
                        //Set defaults for new MyClass()
                        datStart = new DateTime(1900, 1, 1);
                        datEnd = datStart;
                        intParam1 = -1;
                        intParam2 = -1;
                    }
                    
                    IEnumerator IEnumerable.GetEnumerator()
                    {
                        //What goes here?
                    
                        return null;  //Only added this line to stop compiler error
                    }
                    

                    }

                    I know there are many, (many, many,) references to this on the web but I haven't been able to re-work any of them to solve my specific problem. Any help would be MUCH appreciated. Thank you.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    public class MyClass : IEnumerable<put type here> //IEnumerable<t> is better (no boxing) { public System.DateTime datStart; //Schedule start date public System.DateTime datEnd; //Date of final occurrence (#1/1/1900# if N/A) public int intParam1; //Various Uses public int intParam2; //Various Uses //Other fields remove for clarity //Constructor public MyClass() { //Set defaults for new MyClass() datStart = new DateTime(1900, 1, 1); datEnd = datStart; intParam1 = -1; intParam2 = -1; } public IEnumerator<put type here> GetEnumerator() { yield return someStuff; //put more of them here possible in a loop, the compiler will make a state machine out of it } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); //this does not loop, it calls the other GetEnumerator } }

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      Hi, As Thomas said enumerators is not what you need here. You need Reflection, which can give you access to all the data fields, constructors, methods, properties of your class. This is an example listing all the known colors in the SystemColors class:

                      Assembly asm=Assembly.GetAssembly(Color.White.GetType());
                      Type type=asm.GetType("System.Drawing.SystemColors", true);
                      PropertyInfo\[\] pis=type.GetProperties();
                      int n=pis.Length;
                      log("There are "+n+" properties");
                      foreach (PropertyInfo pi in pis) {
                      	Color c=(Color)pi.GetValue(null, null);	// get the SystemColor
                      	log(pi.Name+" = "+c.ToString());
                      }
                      

                      BTW: you can only get information on class members, not on local variables inside some method. :)

                      Luc Pattyn [Forum Guidelines] [My Articles]


                      DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.


                      D Offline
                      D Offline
                      Dowse
                      wrote on last edited by
                      #10

                      Luc, I managed to re-work your example to give me just what I was after. Thanks for putting me on the right track:

                      private void ShowData()
                      {
                      Type type = typeof(MyClass);
                      FieldInfo[] fi = type.GetFields();

                              foreach (FieldInfo info in fi)
                              {
                                  Debug.WriteLine(info.Name + " = " + info.GetValue(varMyClass));
                              }
                          }
                      

                      varMyClass is a variable of type MyClass. Thanks to all who helped.

                      modified on Thursday, July 2, 2009 1:41 PM

                      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