F
FasterThanGravity
Guest
I am returning an enumerator object from the IEnumerable implementation but it is not compiling. It states that I dont implement the interface. I commented out a section, and it compiles if I use that code, but I dont see how I can implement GetEnumerator, without implementing the interface. (If I remove my I Enumerable from my interface implementation list, I can call the GetEnumerator Method somehow.
Edit - The implementation in question is at the very bottom of Spectrum.
namespace ConsoleApplication1 //Iterators as Properties instead of Methods
{
class Spectrum : IEnumerable<string>
{
bool _listfromUVtoIR;
string[] colors = { "violet", "blue", "cyan", "green", "yellow", "orange", "red" };
public Spectrum(bool listFromUVtoIR)
{
_listfromUVtoIR = listFromUVtoIR;
}
/* public IEnumerator<string> GetEnumerator() /*foreach construct calls GetEnumerator() <-- This class is now enumerable
GetEnumerator calls UVtoIR enumerator if bool is true
GetEnumerator calls IRtoUV enumerator if bool is false
*/
/* return _listfromUVtoIR
? UVtoIR
: IRtoUV; */
public IEnumerator<string> UVtoIR //Expects return to be Enumerator which returns strings.
// This is an iterator as it produces Enumerator
{
get
{
for (int i = 0; i < colors.Length; i++)
{
yield return colors;
/* yield return specifies next item in sequence*/
}
}
}
public IEnumerator<string> IRtoUV
{
get
{
for (int i = colors.Length-1;i>=0;i--)
{
yield return colors;
}
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
{
return _listfromUVtoIR
? UVtoIR
: IRtoUV;
}
}
}
class Program
{
static void Main(string[] args)
{
Spectrum spectrum = new Spectrum(true);
Spectrum reverseSpectrum = new Spectrum(false);
foreach (string i in spectrum)
{
Console.WriteLine(i);
}
Console.WriteLine("\t\t\t");
foreach (string i in reverseSpectrum)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
Thanks in advance.
Continue reading...
Edit - The implementation in question is at the very bottom of Spectrum.
namespace ConsoleApplication1 //Iterators as Properties instead of Methods
{
class Spectrum : IEnumerable<string>
{
bool _listfromUVtoIR;
string[] colors = { "violet", "blue", "cyan", "green", "yellow", "orange", "red" };
public Spectrum(bool listFromUVtoIR)
{
_listfromUVtoIR = listFromUVtoIR;
}
/* public IEnumerator<string> GetEnumerator() /*foreach construct calls GetEnumerator() <-- This class is now enumerable
GetEnumerator calls UVtoIR enumerator if bool is true
GetEnumerator calls IRtoUV enumerator if bool is false
*/
/* return _listfromUVtoIR
? UVtoIR
: IRtoUV; */
public IEnumerator<string> UVtoIR //Expects return to be Enumerator which returns strings.
// This is an iterator as it produces Enumerator
{
get
{
for (int i = 0; i < colors.Length; i++)
{
yield return colors;
/* yield return specifies next item in sequence*/
}
}
}
public IEnumerator<string> IRtoUV
{
get
{
for (int i = colors.Length-1;i>=0;i--)
{
yield return colors;
}
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
{
return _listfromUVtoIR
? UVtoIR
: IRtoUV;
}
}
}
class Program
{
static void Main(string[] args)
{
Spectrum spectrum = new Spectrum(true);
Spectrum reverseSpectrum = new Spectrum(false);
foreach (string i in spectrum)
{
Console.WriteLine(i);
}
Console.WriteLine("\t\t\t");
foreach (string i in reverseSpectrum)
{
Console.WriteLine(i);
}
Console.ReadLine();
}
}
}
Thanks in advance.
Continue reading...