Re: foreach pretty useless for composite classes, don't ya thunk?
- From: "Family Tree Mike" <FamilyTreeMike@xxxxxxxxxxxxxxxx>
- Date: Sat, 20 Sep 2008 19:03:47 -0400
Change the code to look like this in your foreach within main:
foreach (string s in ((IEnumerable <string>)nlist))
Console.Out.WriteLine(string.Format("The next name is: {0}", s));
foreach (int i in ((IEnumerable<int>)nlist))
Console.Out.WriteLine(string.Format("The next integer is: {0}", i));
"raylopez99" <raylopez99@xxxxxxxxx> wrote in message news:8a54f896-90d8-4b22-8a64-396328457d80@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Family Tree Mike-- I went through your example, it was quite good, and
have a question, that I think cuts to my concerns about 'foreach'.
Specifically, I'm trying to return multiple sequences using multiple
IEnumerators / multiple IEnumerable and multiple 'yields', but am
getting this compiler error:" foreach statement cannot operate on
variables of type 'IEnumerableEx01.Namer' because it implements
multiple instantiations of System.Collections.Generic.IEnumerable<T>';
try casting to a specific interface instantiation"
This error only occurs when you try to use 'foreach' in main(), so of
course commenting out 'foreach' in main will 'solve' the problem, but
how do I use 'foreach'? Some sort of cast seems to be in order, but
try as I might I could not get it right. A search on the net found
this close near hit but miss:
http://blogs.msdn.com/ericlippert/archive/2007/11/09/covariance-and-contravariance-in-c-part-ten-dealing-with-ambiguity.aspx
I modified your original code to include an array of ints (in addition
to your array of strings).
Rather than talk about it, below is the code, I think you can see
where I'm going.
How to solve this problem? If it can be solved, I take back what I
said about 'foreach' being useless. But, if it cannot be solved, then
my original point stands (this was in fact my original point--that you
should be able to make foreach universal to traverse a complex,
compound class, not just a class with one member variable).
Thanks in advance FTM (or anybody else answering)
RL
//////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace myNamespace1
{
class Program
{
static void Main(string[] args)
{
Namer nlist = new Namer ();
nlist.FirstNames = new string [ ] { "Ray", "Casey",
"Zanaida" };
nlist.LastNames = new string [ ] { "Anthony", "Gonzales",
"Lopez", "Jones" };
//foreach (string s in nlist) //compiler error here
// Console.Out.WriteLine(string.Format("The next name is:
{0}", s));
//Console.In.ReadLine();
}
}
}
//////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace myNamespace1
{
class Namer:IEnumerable<string>,IEnumerable<int> //<--note this
second interface
// everything here compiles correctly, but in main() I cannot use
foreach
{
public string[] FirstNames;
public string[] LastNames;
public int[] iArrNamer; //new member here
public Namer()
{
iArrNamer = new int [2];
iArrNamer[0] = 1;
iArrNamer[1] = 2;
}
IEnumerator<string> IEnumerable<string>.GetEnumerator()
{
for (int idxLast = 0; idxLast < LastNames.Count(); idxLast+
+)
for (int idxFirst = 0; idxFirst < FirstNames.Count();
idxFirst++)
yield return string.Format("{0} {1}",
FirstNames[idxFirst], LastNames[idxLast]);
}
// this GetEnumerator is new
IEnumerator<int> IEnumerable<int>.GetEnumerator()
{
for (int i = 0; i < iArrNamer.Length; i++)
{
yield return iArrNamer[i];
}
}
System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
for (int idxLast = 0; idxLast < LastNames.Count(); idxLast+
+)
//BTW ++idxLast (made globally) also works fine, strangely enough
for (int idxFirst = 0; idxFirst < FirstNames.Count();
idxFirst++)
yield return string.Format("{0} {1}",
FirstNames[idxFirst], lastNames[idxLast]);
for (int i = 0; i < iArrNamer.Length; i++)
{
yield return iArrNamer[i];
}
//compiles OK, since multiple yield returns are allowed, but something
is missing--what?
//I'm trying to return either the string, or, the int, since class
Namer has two member variables, an array of strings and an array of
ints, not just one (originally, there was only an array of strings,
now I've added the array of ints)
}
}
}
//////////////////////////////////////////////////
.
- Follow-Ups:
- Re: foreach pretty useless for composite classes, don't ya thunk?
- From: raylopez99
- Re: foreach pretty useless for composite classes, don't ya thunk?
- References:
- foreach pretty useless for composite classes, don't ya thunk?
- From: raylopez99
- RE: foreach pretty useless for composite classes, don't ya thunk?
- From: Family Tree Mike
- Re: foreach pretty useless for composite classes, don't ya thunk?
- From: raylopez99
- Re: foreach pretty useless for composite classes, don't ya thunk?
- From: Family Tree Mike
- Re: foreach pretty useless for composite classes, don't ya thunk?
- From: raylopez99
- foreach pretty useless for composite classes, don't ya thunk?
- Prev by Date: split html
- Next by Date: Trying to create a dynamic class...?
- Previous by thread: Re: foreach pretty useless for composite classes, don't ya thunk?
- Next by thread: Re: foreach pretty useless for composite classes, don't ya thunk?
- Index(es):
Relevant Pages
|