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