Giving an object multiple enumerator types
-
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.
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 )
-
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 )
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 errorError 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 isforeach (int i in info)
where info is of type Myclass -
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 errorError 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 isforeach (int i in info)
where info is of type MyclassI'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 )
-
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 )
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?
-
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?
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 )
-
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 )
-
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 errorError 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 isforeach (int i in info)
where info is of type MyclassHi, 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