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. Implementing IEnumerable<t></t>

Implementing IEnumerable<t></t>

Scheduled Pinned Locked Moved C#
questiontestingcollaborationbeta-testinghelp
23 Posts 6 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.
  • J Jammer 0

    OK, I know it wasn't very long but i'm stuck again ... I now need to write a custom Add() method to add records to this Custom Generic List, really don't know where to start really. My code runs this line: BuildGenericLists SingleDirectoryFileStore = new BuildGenericLists(); Which executes:

        class BuildGenericLists : IEnumerable
        {
            public struct Filelist
            {
                public string file_name, file_size, file_ext, last_access;
    
                // create a constructor
                public Filelist(string fileName, string fileSize, string fileExt, string lastAccess)
                {
                    file_name = fileName;
                    file_size = fileSize;
                    file_ext = fileExt;
                    last_access = lastAccess;
                }
            }
    
            List<Filelist> MainFileTableObject = new List<Filelist>();
    
            public IEnumerator GetEnumerator()
            {
                foreach (Filelist fileList in MainFileTableObject)
                yield return fileList;
            }
    
            public void Add()
            {
            }
        }
    

    How would I go about fleshing out the stubbed Add() method so that after the initial line above to create the list I can issue add commands to add the relevant values to the list? MainFileTableObject.Add(new Filelist(@"G:\Jammer1\", "2132987KB", ".WAV", "12/12/2001")); TIA,

    J Offline
    J Offline
    Jammer 0
    wrote on last edited by
    #11

    Hi Again, Any help on this would be really appreciated. Thanks, James.

    B 1 Reply Last reply
    0
    • J Jammer 0

      Hi Again, Any help on this would be really appreciated. Thanks, James.

      B Offline
      B Offline
      buchstaben
      wrote on last edited by
      #12

      can you tell us why you want to implement your own IEnumerable type? what special functionality is needed? seems like system.collectionn.generic.list would do the job for you.

      J 1 Reply Last reply
      0
      • B buchstaben

        can you tell us why you want to implement your own IEnumerable type? what special functionality is needed? seems like system.collectionn.generic.list would do the job for you.

        J Offline
        J Offline
        Jammer 0
        wrote on last edited by
        #13

        Hi, Sure, I'm new to C# so bear with me. Basically, my list is eventually going to contain strings, bools, dates, ints, doubles etc ... all sorts basically and I was advised that a generic list would be the way forward and that in order to accommodate those different data types in one list I would need to go down this route ... Also the IEnumerable is required in order to subsequently bind this to an Infragistics datagrid in the application interface. Am I barking up the wrong tree going down this route? Thanks for the reply! James.

        B 1 Reply Last reply
        0
        • J Jammer 0

          Hi All, I've been looking at this for a few hours now, tried a few things I've read on the web with not much luck and I'm a git lost, I've never done this before and I'm getting a bit confused by the examples I've read online to be honest. How do I implement IEnumerable on this code:

          class BuildGenericLists : IEnumerable<T>
          {
              // this defines the structure of the main file table object that will
              // contain the list of files and information gained from the system scan.
              public struct Filelist
              {
                  public string file\_name, file\_size, file\_ext, last\_access;
          
                  // create a constructor
                  public Filelist(string fileName, string fileSize, string fileExt, string lastAccess)
                  {
                      file\_name = fileName;
                      file\_size = fileSize;
                      file\_ext = fileExt;
                      last\_access = lastAccess;
                  }
              }
          
              // this function builds the main table from the structure defined above and returns an
              // empty generic list ready for populating.
              public static object BuildMainFileList()
              {
                  List MainFileTableObject = new List();
          
                  // just adding some test data to the table for testing purposes.
                  MainFileTableObject.Add(new Filelist(@"G:\\Jammer1\\", "2132987KB", ".WAV", "12/12/2001"));
                  MainFileTableObject.Add(new Filelist(@"G:\\Jammer2\\", "2132987KB", ".AIF", "12/12/2002"));
                  MainFileTableObject.Add(new Filelist(@"G:\\Jammer3\\", "2132987KB", ".PRG", "12/12/2003"));
                  MainFileTableObject.Add(new Filelist(@"G:\\Jammer4\\", "2132987KB", ".ASD", "12/12/2004"));
                  MainFileTableObject.Add(new Filelist(@"G:\\Jammer5\\", "2132987KB", ".WAV", "12/12/2005"));
                  MainFileTableObject.Add(new Filelist(@"G:\\Jammer6\\", "2132987KB", ".WAV", "12/12/2006"));
          
                  // return the table
                  return MainFileTableObject;
              }
          }
          

          Should the

          : IEnumerable<T>

          actually be on the following line instead?

          public static object BuildMainFileList();

          Once this has been inherited how would the code for the GetEnumerator() / Current / Move Next be inserted into the code? Sorry I know this is going back to basics for most of you but help a newbie :) The examples on the net all seem to differ and I can't work out what is needed and what isn't needed as there seems to be a

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #14

          You should implement the IEnumerator for the data type that it should enumerate: class BuildGenericLists : IEnumerable<FileList> You don't have to create an enumerator, the List class already has one that you can return when you implement the GetEnumerator method: return MainFileTableObject.GetEnumerator();

          Despite everything, the person most likely to be fooling you next is yourself.

          J 1 Reply Last reply
          0
          • G Guffa

            You should implement the IEnumerator for the data type that it should enumerate: class BuildGenericLists : IEnumerable<FileList> You don't have to create an enumerator, the List class already has one that you can return when you implement the GetEnumerator method: return MainFileTableObject.GetEnumerator();

            Despite everything, the person most likely to be fooling you next is yourself.

            J Offline
            J Offline
            Jammer 0
            wrote on last edited by
            #15

            So can a standard List<> hold values of string, int, bool etc at the same time? I was of the impression that a I needed to 'design' my own List<> in order to do this?

            G 1 Reply Last reply
            0
            • J Jammer 0

              So can a standard List<> hold values of string, int, bool etc at the same time? I was of the impression that a I needed to 'design' my own List<> in order to do this?

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #16

              A List<object> can hold any kind of data. You might want to inherit or encapsulate the list and implement some methods to simplify dealing with different objetcs. Another alternative is to create a class that encapsulates an object reference and has methods to handle the different kind of data, and make a list containing items of that class.

              Despite everything, the person most likely to be fooling you next is yourself.

              J 1 Reply Last reply
              0
              • G Guffa

                A List<object> can hold any kind of data. You might want to inherit or encapsulate the list and implement some methods to simplify dealing with different objetcs. Another alternative is to create a class that encapsulates an object reference and has methods to handle the different kind of data, and make a list containing items of that class.

                Despite everything, the person most likely to be fooling you next is yourself.

                J Offline
                J Offline
                Jammer 0
                wrote on last edited by
                #17

                Ahhh, OK ... thats good to hear as I've dumped all my old code and started again but I'm having problems with that now ... :( OK here goes again ... I'm still lost on this! I've ditched my old classes and started again, I just couldn't fathom what was wrong or what to do and now I'm having problem with my new implementation. I've written this class:

                public class BuildFileList<T> : IEnumerable<T>
                {
                    private List<T> fileList = new List<T>();
                
                    public T GetFile(int pos)
                    {
                        return fileList\[pos\];
                    }
                
                    public void AddFile(T c)
                    {
                        fileList.Add(c);
                    }
                
                    public void ClearFiles()
                    {
                        fileList.Clear();
                    }
                
                    public int Count
                    {
                        get { return fileList.Count; }
                    }
                
                    IEnumerator<T> IEnumerable<T>.GetEnumerator()
                    {
                        return fileList.GetEnumerator();
                    }
                
                    IEnumerator IEnumerable.GetEnumerator()
                    {
                        return fileList.GetEnumerator();
                    }
                }
                

                but using these commands:

                BuildFileList<FileList> SingleDirectoryFileStore = new BuildFileList<FileList>();

                SingleDirectoryFileStore.AddFile(@"G:\Jammer1\", "2132987KB", ".WAV", "12/12/2001");

                Gets two errors:

                The type or namespace name 'FileList' could not be found (are you missing a using directive or an assembly reference?)

                and

                No overload for method 'AddFile' takes '4' arguments

                I thought the whole point of using List<T> was that it was generic and you didn't need to tell it in advance what to expect? Man, this whole generics thing has been hurting my head all day, I've not made any progress at all really. Any help on this would be great Guffa I'm completely lost and losing the will to live!

                E 1 Reply Last reply
                0
                • J Jammer 0

                  Ahhh, OK ... thats good to hear as I've dumped all my old code and started again but I'm having problems with that now ... :( OK here goes again ... I'm still lost on this! I've ditched my old classes and started again, I just couldn't fathom what was wrong or what to do and now I'm having problem with my new implementation. I've written this class:

                  public class BuildFileList<T> : IEnumerable<T>
                  {
                      private List<T> fileList = new List<T>();
                  
                      public T GetFile(int pos)
                      {
                          return fileList\[pos\];
                      }
                  
                      public void AddFile(T c)
                      {
                          fileList.Add(c);
                      }
                  
                      public void ClearFiles()
                      {
                          fileList.Clear();
                      }
                  
                      public int Count
                      {
                          get { return fileList.Count; }
                      }
                  
                      IEnumerator<T> IEnumerable<T>.GetEnumerator()
                      {
                          return fileList.GetEnumerator();
                      }
                  
                      IEnumerator IEnumerable.GetEnumerator()
                      {
                          return fileList.GetEnumerator();
                      }
                  }
                  

                  but using these commands:

                  BuildFileList<FileList> SingleDirectoryFileStore = new BuildFileList<FileList>();

                  SingleDirectoryFileStore.AddFile(@"G:\Jammer1\", "2132987KB", ".WAV", "12/12/2001");

                  Gets two errors:

                  The type or namespace name 'FileList' could not be found (are you missing a using directive or an assembly reference?)

                  and

                  No overload for method 'AddFile' takes '4' arguments

                  I thought the whole point of using List<T> was that it was generic and you didn't need to tell it in advance what to expect? Man, this whole generics thing has been hurting my head all day, I've not made any progress at all really. Any help on this would be great Guffa I'm completely lost and losing the will to live!

                  E Offline
                  E Offline
                  Elina Blank
                  wrote on last edited by
                  #18

                  You should specify what is . (This is what Guffa had mentioned to you previously) The generics means, that it will hold just that type of objects, and you do not need to cast it, when getting back. (you were holding objects before Generics)

                  Sincerely, Elina Life is great!!! Enjoy every moment of it! :-O

                  J 1 Reply Last reply
                  0
                  • E Elina Blank

                    You should specify what is . (This is what Guffa had mentioned to you previously) The generics means, that it will hold just that type of objects, and you do not need to cast it, when getting back. (you were holding objects before Generics)

                    Sincerely, Elina Life is great!!! Enjoy every moment of it! :-O

                    J Offline
                    J Offline
                    Jammer 0
                    wrote on last edited by
                    #19

                    Thanks for that! Just 30 seconds before your mail came in I cracked it ... what a painful day! So glad I've now made some progress on this thought, I was starting to go mad with this issue! Thanks for everyone's help and sorry for the daft questions and so on.

                    1 Reply Last reply
                    0
                    • J Jammer 0

                      Hi, Sure, I'm new to C# so bear with me. Basically, my list is eventually going to contain strings, bools, dates, ints, doubles etc ... all sorts basically and I was advised that a generic list would be the way forward and that in order to accommodate those different data types in one list I would need to go down this route ... Also the IEnumerable is required in order to subsequently bind this to an Infragistics datagrid in the application interface. Am I barking up the wrong tree going down this route? Thanks for the reply! James.

                      B Offline
                      B Offline
                      buchstaben
                      wrote on last edited by
                      #20

                      hm..i'm still not sure if you got it now (looking at the other replies). using a generic collection is mainly useful to have a container with elements of the same type. if you want a list with almost any datatype in it, generics is not helpful. using List list = new List(); will create a list containing only strings. so list.Add will only allow you to add strings and getting elements of this list will always return a string. if all you want is a collection of FileList instances, use List. you will not need to implement ienumerable. if you want a collection of instances of any type, use untyped system.collection.list. you may also want to specify what concrete data you want to hold in that collection, so a better advise can be given.

                      J 1 Reply Last reply
                      0
                      • B buchstaben

                        hm..i'm still not sure if you got it now (looking at the other replies). using a generic collection is mainly useful to have a container with elements of the same type. if you want a list with almost any datatype in it, generics is not helpful. using List list = new List(); will create a list containing only strings. so list.Add will only allow you to add strings and getting elements of this list will always return a string. if all you want is a collection of FileList instances, use List. you will not need to implement ienumerable. if you want a collection of instances of any type, use untyped system.collection.list. you may also want to specify what concrete data you want to hold in that collection, so a better advise can be given.

                        J Offline
                        J Offline
                        Jammer 0
                        wrote on last edited by
                        #21

                        Hi buchstaben, This is what I have now (got it working last night):

                        using System;
                        using System.Collections;
                        using System.Collections.Generic;
                        using System.Linq;
                        using System.Text;

                        namespace SampleSort.DataLayer
                        {
                        public class BuildFileList<T> : IEnumerable<T>
                        {
                        private List<T> fileList = new List<T>();

                            public T Get(int pos)
                            {
                                return fileList\[pos\];
                            }
                        
                            public void Add(T c)
                            {
                                fileList.Add(c);
                            }
                        
                            public void Clear()
                            {
                                fileList.Clear();
                            }
                        
                            public int Count
                            {
                                get { return fileList.Count; }
                            }
                        
                            IEnumerator<T> IEnumerable<T>.GetEnumerator()
                            {
                                return fileList.GetEnumerator();
                            }
                        
                            IEnumerator IEnumerable.GetEnumerator()
                            {
                                return fileList.GetEnumerator();
                            }
                        }
                        
                        public class FileEntry
                        {
                            private string \_fileName;
                            private string \_fileSize;
                            private string \_fileExt;
                            private string \_fileDate;
                        
                            public FileEntry(string \_fileName, string \_fileSize, string \_fileExt, string \_fileDate)
                            {
                                this.\_fileName = \_fileName;
                                this.\_fileSize = \_fileSize;
                                this.\_fileExt = \_fileExt;
                                this.\_fileDate = \_fileDate;
                            }
                        
                            public string FileName
                            {
                                get { return \_fileName;}
                                set {\_fileName = value;}
                            }
                            public string FileSize
                            {
                                get { return \_fileSize; }
                                set { \_fileSize = value; }
                            }
                            public string FileExt
                            {
                                get { return \_fileExt; }
                                set { \_fileExt = value; }
                            }
                            public string FileDate
                            {
                                get { return \_fileDate; }
                                set { \_fileDate = value; }
                            }
                        }
                        

                        }

                        Like I said even though this is only using strings at the moment its going to be expanded to include other value types (bool, ints, double etc...) This is being used by:

                            public static List<FileEntry> ScanDirectories(string scanEntryPoint)
                            {
                        
                                // build the store
                                //BuildGenericLists SingleDirectoryFileStore = new BuildGenericLists();
                                List<FileEntry> SingleDirectoryFileStore = new List<FileEntry>();
                        
                                // grab the start location of the recursion
                                DirectoryInfo S
                        
                        B 1 Reply Last reply
                        0
                        • J Jammer 0

                          Hi buchstaben, This is what I have now (got it working last night):

                          using System;
                          using System.Collections;
                          using System.Collections.Generic;
                          using System.Linq;
                          using System.Text;

                          namespace SampleSort.DataLayer
                          {
                          public class BuildFileList<T> : IEnumerable<T>
                          {
                          private List<T> fileList = new List<T>();

                              public T Get(int pos)
                              {
                                  return fileList\[pos\];
                              }
                          
                              public void Add(T c)
                              {
                                  fileList.Add(c);
                              }
                          
                              public void Clear()
                              {
                                  fileList.Clear();
                              }
                          
                              public int Count
                              {
                                  get { return fileList.Count; }
                              }
                          
                              IEnumerator<T> IEnumerable<T>.GetEnumerator()
                              {
                                  return fileList.GetEnumerator();
                              }
                          
                              IEnumerator IEnumerable.GetEnumerator()
                              {
                                  return fileList.GetEnumerator();
                              }
                          }
                          
                          public class FileEntry
                          {
                              private string \_fileName;
                              private string \_fileSize;
                              private string \_fileExt;
                              private string \_fileDate;
                          
                              public FileEntry(string \_fileName, string \_fileSize, string \_fileExt, string \_fileDate)
                              {
                                  this.\_fileName = \_fileName;
                                  this.\_fileSize = \_fileSize;
                                  this.\_fileExt = \_fileExt;
                                  this.\_fileDate = \_fileDate;
                              }
                          
                              public string FileName
                              {
                                  get { return \_fileName;}
                                  set {\_fileName = value;}
                              }
                              public string FileSize
                              {
                                  get { return \_fileSize; }
                                  set { \_fileSize = value; }
                              }
                              public string FileExt
                              {
                                  get { return \_fileExt; }
                                  set { \_fileExt = value; }
                              }
                              public string FileDate
                              {
                                  get { return \_fileDate; }
                                  set { \_fileDate = value; }
                              }
                          }
                          

                          }

                          Like I said even though this is only using strings at the moment its going to be expanded to include other value types (bool, ints, double etc...) This is being used by:

                              public static List<FileEntry> ScanDirectories(string scanEntryPoint)
                              {
                          
                                  // build the store
                                  //BuildGenericLists SingleDirectoryFileStore = new BuildGenericLists();
                                  List<FileEntry> SingleDirectoryFileStore = new List<FileEntry>();
                          
                                  // grab the start location of the recursion
                                  DirectoryInfo S
                          
                          B Offline
                          B Offline
                          buchstaben
                          wrote on last edited by
                          #22

                          in the code you provided, i cant see any use of your class BuildFileList (thats good, since i still believe it is not needed in your case). i've never worked with WPF, but i think i can give you a hint anyway. first, in your button click event handler, dont specify the return value as object. you know it is a List<FileInfo>, so you can use it. to bind this to your ui control, you have to tell the control the bound type. this will be FileEntry, since FileEntry contains the relevant information, not your list. let's take a gridview as user control. Each row will represent a FileEntry, so you need to specify FileEntry as bound type. if you do that at design time, your gridview will autmatically get columns for each property of FileEntry. having this done, simply say myGridView.DataSource = singleDirectoryResults (assuming that singleDirectoryResults is of type List<FileInfo>.

                          Jammer wrote:

                          All the binding examples I have seen link into a class (such as BuildFileList) which in my case is an empty definition of an object.

                          you will need to linke to FileEntry class. hope this is not too weird, since i dont have any dev environment here.

                          J 1 Reply Last reply
                          0
                          • B buchstaben

                            in the code you provided, i cant see any use of your class BuildFileList (thats good, since i still believe it is not needed in your case). i've never worked with WPF, but i think i can give you a hint anyway. first, in your button click event handler, dont specify the return value as object. you know it is a List<FileInfo>, so you can use it. to bind this to your ui control, you have to tell the control the bound type. this will be FileEntry, since FileEntry contains the relevant information, not your list. let's take a gridview as user control. Each row will represent a FileEntry, so you need to specify FileEntry as bound type. if you do that at design time, your gridview will autmatically get columns for each property of FileEntry. having this done, simply say myGridView.DataSource = singleDirectoryResults (assuming that singleDirectoryResults is of type List<FileInfo>.

                            Jammer wrote:

                            All the binding examples I have seen link into a class (such as BuildFileList) which in my case is an empty definition of an object.

                            you will need to linke to FileEntry class. hope this is not too weird, since i dont have any dev environment here.

                            J Offline
                            J Offline
                            Jammer 0
                            wrote on last edited by
                            #23

                            Thanks for this ... I'm just off making some changes and trying a few things you suggest. Thank! :)

                            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