Implementing IEnumerable<t></t>
-
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
-
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
List<Filelist> list = new List<Filelist>();
only two letters away from being an asset
-
List<Filelist> list = new List<Filelist>();
only two letters away from being an asset
Hi, Damn, I'm still getting used to the posting code bit ... My code is already contains:
List<Filelist> MainFileTableObject = new List<Filelist>();
I'm just struggling with where to put the GetEnumerator() code and the other features on the IEnumerable interface methods. I'm really stuck on this and have been going round in circles all day trying to get to grips with it. Thanks, -
List<Filelist> list = new List<Filelist>();
only two letters away from being an asset
Hi, I've got this to compile now but how do I know if this is actually implementing IEnumerable?
class BuildGenericLists : IEnumerable { // 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 method builds the main table from the structure defined above and returns an // empty generic list ready for populating. public static object BuildMainFileList() { List<Filelist> MainFileTableObject = new List<Filelist>(); // return the empty list return MainFileTableObject; } public IEnumerator GetEnumerator() { return (IEnumerator)this; } }
Using breakpoints I can't see the program ever hitting thereturn (IEnumerator)this;
line. I'm feeling pretty dumb at the moment. I thought I was starting to get a grip of C# basics but I'm stumped on this at the moment. -
Hi, I've got this to compile now but how do I know if this is actually implementing IEnumerable?
class BuildGenericLists : IEnumerable { // 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 method builds the main table from the structure defined above and returns an // empty generic list ready for populating. public static object BuildMainFileList() { List<Filelist> MainFileTableObject = new List<Filelist>(); // return the empty list return MainFileTableObject; } public IEnumerator GetEnumerator() { return (IEnumerator)this; } }
Using breakpoints I can't see the program ever hitting thereturn (IEnumerator)this;
line. I'm feeling pretty dumb at the moment. I thought I was starting to get a grip of C# basics but I'm stumped on this at the moment.the code fragment public IEnumerator GetEnumerator () { return (IEnumerator ) this; } is an infinite loop . this is not going to work . try public IEnumerator GetEnumerator () { return MainFileTableObject ; } Ilist already implement inumerable . Enumerable is little complicated at start . but then get it . you can also use public IEnumerator GetEnumerator () { foreach ( FileList fileList in MainFileTableObject ) yield return fileList ; } // i have not installed Visual Studio right now and dont test sysntax
f(yf) = yf
-
the code fragment public IEnumerator GetEnumerator () { return (IEnumerator ) this; } is an infinite loop . this is not going to work . try public IEnumerator GetEnumerator () { return MainFileTableObject ; } Ilist already implement inumerable . Enumerable is little complicated at start . but then get it . you can also use public IEnumerator GetEnumerator () { foreach ( FileList fileList in MainFileTableObject ) yield return fileList ; } // i have not installed Visual Studio right now and dont test sysntax
f(yf) = yf
Ahhhh ... Thanks for the reply. Just before your reply came through I was repeatedly trying the code in various configs and then noticed that hovering over my code I now say that the class was getting an IEnumerable added but with the following error when I expanded the node:
base {System.Exception} = {"Unable to cast object of type 'SampleSort.DataLayer.BuildGenericLists' to type 'System.Collections.IEnumerator'."}
Changing to your suggestion:
public IEnumerator GetEnumerator () { return MainFileTableObject ; }
Now makes the compiler report:Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.IEnumerator'. An explicit conversion exists (are you missing a cast?)
I'm assuming that's progress! :) I've seen examples using this yield keyword, that's new to C# 3.0 isn't it? (Which I'm using) so it would be good to implement the newer way of doing things. I'm sure this is also due to my shallow understanding at the moment but my code is just producing an empty generic list so doing a foreach over it will add what exectly? Or is this code here in order to build in that functionality to the object rather than actually perform a foreach on an empty list? Thanks loads for your help. -
the code fragment public IEnumerator GetEnumerator () { return (IEnumerator ) this; } is an infinite loop . this is not going to work . try public IEnumerator GetEnumerator () { return MainFileTableObject ; } Ilist already implement inumerable . Enumerable is little complicated at start . but then get it . you can also use public IEnumerator GetEnumerator () { foreach ( FileList fileList in MainFileTableObject ) yield return fileList ; } // i have not installed Visual Studio right now and dont test sysntax
f(yf) = yf
Using:
public IEnumerator GetEnumerator() { foreach (Filelist fileList in MainFileTableObject) yield return fileList; }
Doesn't produce the compile error!! Hmm ... You right, this has been a complicated issue for me to get my head around. Hopefully this is going to work and I can move onto the next thing I don't understand yet! -
Using:
public IEnumerator GetEnumerator() { foreach (Filelist fileList in MainFileTableObject) yield return fileList; }
Doesn't produce the compile error!! Hmm ... You right, this has been a complicated issue for me to get my head around. Hopefully this is going to work and I can move onto the next thing I don't understand yet!Implementing IEnumerable , IEnumerator or even Ilist , ICollection is suppose to be an advance topic so you are excuse not o no .i had the same problem . keep programming . if you stack someware else make a question
f(yf) = yf
-
Implementing IEnumerable , IEnumerator or even Ilist , ICollection is suppose to be an advance topic so you are excuse not o no .i had the same problem . keep programming . if you stack someware else make a question
f(yf) = yf
That is reassuring to know. I had made the assumption this was a pretty basic function of C# as Generics are used a lot as is the foreach functionality. I guess I'm making a bit more progress than I was giving myself credit for. Thanks for this papadimitriou, you've been a great help. :)
-
Implementing IEnumerable , IEnumerator or even Ilist , ICollection is suppose to be an advance topic so you are excuse not o no .i had the same problem . keep programming . if you stack someware else make a question
f(yf) = yf
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, -
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, -
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.
-
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.
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.
-
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
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.
-
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.
-
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?
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.
-
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.
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!
-
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!
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
-
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
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.
-
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.
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.