Re: foreach pretty useless for composite classes, don't ya thunk?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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)

}


}
}

//////////////////////////////////////////////////

.



Relevant Pages

  • Re: System.AccessViolationException
    ... internal string theAP; ... DataColumn col = new DataColumn; ... private string _pwd; ... foreach ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: foreach statement output
    ... by using a 'for' statement instead of the foreach loop: ... and I will use the array to generate a string. ... know about foreach loop workings. ...
    (comp.infosystems.www.authoring.cgi)
  • Re: Trouble Using System.Array.ForEach
    ... The reason that the Array.ForEach Generic method is not suitable for your purposes in this case is that on each iteration of the 'hidden' loop, each element of the array is paseed to the Action delegate Sub, ByVal. ... Therefore the string that is passed to the delegate is returned unaltered and the parent array is not modified. ... And you may be right about the ForEach not fitting my conditions, I really can't say for sure since I have never had the opportunity to use ForEach, but either way, it never hurts to have extra knowledge so that if a situation were to come up where it was fit I would know it. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: For vs. For Each
    ... >> we were told that FOREACH is not a good idea on collections ... > actually have a strongly-typed interface to it and stores the ints ... > without boxing, where an iterator would need to box. ... My first thought was "Wouldn't that be the case with any array of value ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: For vs. For Each
    ... >> we were told that FOREACH is not a good idea on collections ... > actually have a strongly-typed interface to it and stores the ints ... > without boxing, where an iterator would need to box. ... My first thought was "Wouldn't that be the case with any array of value ...
    (microsoft.public.dotnet.languages.vb)