473,397 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

List of enumerators?

Hi,
I have various lists I want to go through, at different paces and I
want to control that with a list of enumerators.

I have the following:

List<List<DataEntry>mydata = new List<List<Data>>();
List<List<DataEntry>.Enumeratormyenums = news
List<List<Data>.Enumertor>();

I do
myenums.Add(mydata[0].GetEnumerator());
then
myenums[0].MoveNext();

well it seems that doesn't work... like myenums[0] is a temporary copy
of the enumerator... I don't know but when I do that, myenums[0]
desn't actually move.

e = myenums[0];
e.MoveNext();

works though, meaning e moves, but it doesn't change myenums[0]
( !?? )

Can someone explain me the rational here and a good way to do what I
am trying to achieve?
Thanks!

Mar 21 '07 #1
12 2102
On Mar 21, 1:39 pm, "A.B." <a...@mytrashmail.comwrote:
I have various lists I want to go through, at different paces and I
want to control that with a list of enumerators.
<snip>

Please post a short but complete program that demonstrates the
problem.
See http://pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon

Mar 21 '07 #2
A.B. wrote:
Hi,
I have various lists I want to go through, at different paces and I
want to control that with a list of enumerators.

I have the following:

List<List<DataEntry>mydata = new List<List<Data>>();
List<List<DataEntry>.Enumeratormyenums = news
List<List<Data>.Enumertor>();

I do
myenums.Add(mydata[0].GetEnumerator());
then
myenums[0].MoveNext();

well it seems that doesn't work... like myenums[0] is a temporary copy
of the enumerator... I don't know but when I do that, myenums[0]
desn't actually move.

e = myenums[0];
e.MoveNext();

works though, meaning e moves, but it doesn't change myenums[0]
( !?? )

Can someone explain me the rational here and a good way to do what I
am trying to achieve?
Thanks!
What you are experiencing is the usage of a value type. An enumerator is
a structure, which means that whenever you get the enumerator from the
list, you are getting a copy of it. If you want the enumerator in the
list to be updated, you have to put the updated enumerator back into the
list:

e = myenums[0];
e.MoveNext();
myenums[0] = e;

--
Göran Andersson
_____
http://www.guffa.com
Mar 21 '07 #3
Here's a working code showing what happens
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<List<int>l = new List<List<int>>();
List<List<int>.Enumeratore = new
List<List<int>.Enumerator>();

l.Add(new List<int>());
l.Add(new List<int>());

l[0].Add(1);
l[0].Add(2);
l[1].Add(10);
l[1].Add(11);

e.Add(l[0].GetEnumerator());
e.Add(l[1].GetEnumerator());

e[0].MoveNext();
Console.WriteLine(e[0].Current); // prints 0, I'd like to
print "1"
List<int>.Enumerator f = e[1];
f.MoveNext(); f.MoveNext();
Console.WriteLine("{0} {1}",f.Current,e[1].Current); //
prints 11 0, I'd like to print "11 11"
}
}
}

Mar 21 '07 #4
A.B. wrote:
List<List<DataEntry>mydata = new List<List<Data>>();
List<List<DataEntry>.Enumeratormyenums = news
myenums.Add(mydata[0].GetEnumerator());
myenums[0].MoveNext();
List<T>.Enumerator is a value type, so myenums[0] will return a copy.
Cast it to IEnumerable<Tinstead.

-- Barry

--
http://barrkel.blogspot.com/
Mar 21 '07 #5
On Mar 21, 2:07 pm, Göran Andersson <g...@guffa.comwrote:

<snip>
What you are experiencing is the usage of a value type. An enumerator is
a structure, which means that whenever you get the enumerator from the
list, you are getting a copy of it.
Note that enumerators aren't *in general* structures. It just happens
that List<T>.Enumerator is a structure.

That's a pretty nasty "gotcha" when converting from .NET 1.1 to
generics...

Jon

Mar 21 '07 #6
On Mar 21, 10:27 am, Barry Kelly <barry.j.ke...@gmail.comwrote:
List<T>.Enumerator is a value type, so myenums[0] will return a copy.
Cast it to IEnumerable<Tinstead.
Could you show me how I'd do that on the sample code I posted?
Thanks!

Mar 21 '07 #7
A.B. <ab*@mytrashmail.comwrote:
On Mar 21, 10:27 am, Barry Kelly <barry.j.ke...@gmail.comwrote:
List<T>.Enumerator is a value type, so myenums[0] will return a copy.
Cast it to IEnumerable<Tinstead.

Could you show me how I'd do that on the sample code I posted?
Thanks!
You only need to change the declarations for e and f:

List<IEnumerator<int>e = new List<IEnumerator<int>>();
and
IEnumerator<intf = e[1];

Then it works as per your comments.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 21 '07 #8

"A.B." <ab*@mytrashmail.comwrote in message
news:11*********************@l75g2000hse.googlegro ups.com...
Hi,
I have various lists I want to go through, at different paces and I
want to control that with a list of enumerators.

I have the following:

List<List<DataEntry>mydata = new List<List<Data>>();
List<List<DataEntry>.Enumeratormyenums = news
List<List<Data>.Enumertor>();

I do
myenums.Add(mydata[0].GetEnumerator());
then
myenums[0].MoveNext();

well it seems that doesn't work... like myenums[0] is a temporary copy
of the enumerator... I don't know but when I do that, myenums[0]
desn't actually move.
Correct. Accessing an item inside a List always returns a temporary copy,
while access to an array element is in-place. For reference types, a copy
of the reference means you end up working on the same object in the end, but
with value types, a copy is completely independent.
Mar 21 '07 #9
Tried it but

e.Add(l[0].GetEnumerator());

fails

Error 1 The best overloaded method match for
'System.Collections.Generic.List<System.Collection s.Generic.IEnumerable<int>>.Add(System.Collections .Generic.IEnumerable<int>)'
has some invalid arguments

and

e.Add((IEnumerable<int>)l[0].GetEnumerator());

fails too

Error 1 Cannot convert type
'System.Collections.Generic.List<int>.Enumerator' to
'System.Collections.Generic.IEnumerable<int>'
Mar 21 '07 #10
A.B. wrote:
Tried it but

e.Add(l[0].GetEnumerator());

fails

Error 1 The best overloaded method match for
'System.Collections.Generic.List<System.Collection s.Generic.IEnumerable<int>>.Add(System.Collections .Generic.IEnumerable<int>)'
has some invalid arguments
I made a couple of mistakes, you should instantiate:

List<IEnumerator<int>>

rather than:

List<IEnumerable<int>>

Sorry.

-- Barry

--
http://barrkel.blogspot.com/
Mar 21 '07 #11
A.B. wrote:
Here's a working code showing what happens
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<List<int>l = new List<List<int>>();
List<List<int>.Enumeratore = new
List<List<int>.Enumerator>();

l.Add(new List<int>());
l.Add(new List<int>());

l[0].Add(1);
l[0].Add(2);
l[1].Add(10);
l[1].Add(11);

e.Add(l[0].GetEnumerator());
e.Add(l[1].GetEnumerator());

e[0].MoveNext();
As I said, you are getting a copy of the enumerator. If you want the
enumerator in the list to change, you have to put the enumerator that
you changed back in the list:

Enumerator en = e[0];
en.MoveNext();
e[0] = en;
Console.WriteLine(e[0].Current); // prints 0, I'd like to
print "1"
List<int>.Enumerator f = e[1];
f.MoveNext(); f.MoveNext();
Console.WriteLine("{0} {1}",f.Current,e[1].Current); //
prints 11 0, I'd like to print "11 11"
}
}
}

--
Göran Andersson
_____
http://www.guffa.com
Mar 21 '07 #12
Göran Andersson <gu***@guffa.comwrote:

<snip>
As I said, you are getting a copy of the enumerator. If you want the
enumerator in the list to change, you have to put the enumerator that
you changed back in the list:

Enumerator en = e[0];
en.MoveNext();
e[0] = en;
Or make the list a list of boxed versions which can be used directly
via an interface, in which case there's no need to replace anything -
the value inside the box changes instead.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 21 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bob Shafer | last post by:
Is it possible to create dynamic enumerators in Visual Basic .Net? For example, an enumurator that provides a dropdown of available SQL servers?
6
by: Matt Taylor | last post by:
I'm trying to write an x86 assembler in C++ for use in a debugger. What I'd like do is to use template specialization to prevent invalid combinations from compiling. Thus one could not accidentally...
1
by: Dave | last post by:
I have several enums that are generated by a code generator (and I have no control over the code generator), the problem is that the names are pretty long enum VeryLoooooooongEnumName {...
27
by: Tripper | last post by:
Which is the better way to go and why? //trivial example List<string> strings = GetStrings(); foreach (string s in strings) { // some operation; } strings.ForEach(
6
by: Steven D'Aprano | last post by:
If I want to iterate over part of the list, the normal Python idiom is to do something like this: alist = range(50) # first item is special x = alist # iterate over the rest of the list for...
15
by: Macca | last post by:
Hi, My app needs to potentially store a large number of custom objects and be able to iterate through them quickly. I was wondering which data structure would be the most efficient to do this,a...
1
muaddubby
by: muaddubby | last post by:
Hello all and happy new year. I've seen several posts floating around asking about string enumerators in C#, and generally speaking, they're not supported. I've come up with a way around it...
1
nitindel
by: nitindel | last post by:
Hi All, May i have an in depth article of enumerators in C#..that explains the Enumerators in C# very precisely.... Thanks, Nitin
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.