473,396 Members | 1,923 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,396 software developers and data experts.

VS2005/c#2.0: enum and user control problems

I'm using VS2005 beta 2.

(1) It looks like "foreach" is looking for an IEnumerable only, and not an
IEnumerator.
That means that class I use in the interation has to have GetEnumerator(),
rather than supporting the IEnumerator protocol (incl. automatically created
generator class using "yield return".) I can define GetEnumerator(), but
this forces me into one implementation (or an ugly class with state that
swiches and calls a sub-enumerator), rather than calling the IEnumerator
directly.
So I can't do:
public IEnumerator listForwards() { yield return 1; yield return 2; }
public IEnumerator listBackwards() { yield return 2; yield return 1; }
foreach ( int i in listForwards()) { ... }
but I'm forced to write:
public IEnumerator GetEnumerator() { return listForwards(); }
to get things to work, boxing me in to what GE() returns.
It's already bad enough that generator functions in c# are restricted to
being enumerators, I hope things aren't even more screwy.
Googling shows that the fomer should work in 2.0. (I know there's an issue
about not having a default GetEnumerator() in the new collections, but
that's a different issue.)

(2) I cannot create an empty winform user control, add it to the toolbox,
and drop it on a form -- either nothing happens, or an error is generated
about not being able to load the control (without other explanation.) The
same exersize works fine (obviously) in VS2003.
Please let me know if I'm misunderstanding things here or have done
something silly, or if the release bits will solve these problems...

thanks,
m
Nov 28 '05 #1
6 1389

"Mike" <vi********@yahoo.com> wrote in message
news:Od**************@TK2MSFTNGP14.phx.gbl...
(1) It looks like "foreach" is looking for an IEnumerable only, and not an
IEnumerator.
That means that class I use in the interation has to have GetEnumerator(),

....

Not really -- this works for me, but maybe I'm missing the point -- just
type the return as IEnumerable:
public class RaceGrid
{
int[] _rg = new int[] { 1, 2 };
public IEnumerable ListForward()
{
for(int i = 0; i < _rg.Length; ++i)
yield return _rg[i];
}
}

Client-side code looks like:
RaceGrid rg = new RaceGrid();
foreach(int i in rg.ListForward())
{
Console.WriteLine(i);
}

--
Mickey Williams
C# MVP, MCT, Author
http://www.servergeek.com/blogs/mickey
Nov 29 '05 #2
Mike wrote:
I'm using VS2005 beta 2.

(1) It looks like "foreach" is looking for an IEnumerable only, and not an
IEnumerator.
That means that class I use in the interation has to have GetEnumerator(),


Simply add a GetEnumerator() method that returns this:

public class SomeClass<T> : IEnumerator<T>, IEnumerable<T>
{
....

public IEnumerator<T> GetEnumerator()
{
return this;
}

....
}

(this assumes you're using .NET 2.0 and that the class implements
IEnumerator<T>, if not, just remove <T>).

Of course, doing it this way as opposed to constructing a new enumerator
class wrapping your object means that only one enumerator can be used at
any given time against such an object.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 30 '05 #3

"Lasse Vågsæther Karlsen" <la***@vkarlsen.no> wrote in message
news:OH**************@TK2MSFTNGP09.phx.gbl...
Mike wrote:
I'm using VS2005 beta 2.

(1) It looks like "foreach" is looking for an IEnumerable only, and not
an IEnumerator.
That means that class I use in the interation has to have
GetEnumerator(),
Simply add a GetEnumerator() method that returns this:

public class SomeClass<T> : IEnumerator<T>, IEnumerable<T>
{
....

public IEnumerator<T> GetEnumerator()
{
return this;
}


The key word there is "simply". The whole issue is that your class may have
multiple enumerators.
(For example, c.EnumInOrder(), c.EnumSorted(), c.EnumWithFilter("foo"),
etc.)
C# does not have Java-style non-static inner classes (which is fine with me
in general) so this forces the use of some other pattern (such as a class
which "switches" or several sub-classes) to get differrent enumerators - all
of which requires more code than necessary. (If it weren't for the fact the
just returning IEnumerable from the generator works, whether it make sense
or not...)


....
}

(this assumes you're using .NET 2.0 and that the class implements
IEnumerator<T>, if not, just remove <T>).

Of course, doing it this way as opposed to constructing a new enumerator
class wrapping your object means that only one enumerator can be used at
any given time against such an object.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2

Nov 30 '05 #4

"Mickey Williams [C# MVP]" <myfirstnameatservergeekdotcom> wrote in message
news:OE****************@TK2MSFTNGP09.phx.gbl...

"Mike" <vi********@yahoo.com> wrote in message
news:Od**************@TK2MSFTNGP14.phx.gbl...
(1) It looks like "foreach" is looking for an IEnumerable only, and not
an IEnumerator.
That means that class I use in the interation has to have
GetEnumerator(), ...

Not really -- this works for me, but maybe I'm missing the point -- just
type the return as IEnumerable:


Yes, this seems to work. The point is -- this is, if not a bug, at least a
wart.
The protocol that is encapsulated by IEnumerator (Current, MoveNext()...) is
all that is required for "foreach" to work.
It would makes sense to use IEnumerable as backup if the target for
"foreach" did not support IEnumerator, but it should not require it.

m
public class RaceGrid
{
int[] _rg = new int[] { 1, 2 };
public IEnumerable ListForward()
{
for(int i = 0; i < _rg.Length; ++i)
yield return _rg[i];
}
}

Client-side code looks like:
RaceGrid rg = new RaceGrid();
foreach(int i in rg.ListForward())
{
Console.WriteLine(i);
}

--
Mickey Williams
C# MVP, MCT, Author
http://www.servergeek.com/blogs/mickey

Nov 30 '05 #5
"Mike" <vi********@yahoo.com> wrote in message
news:Og**************@TK2MSFTNGP14.phx.gbl...
The key word there is "simply". The whole issue is that your class may
have multiple enumerators.
(For example, c.EnumInOrder(), c.EnumSorted(), c.EnumWithFilter("foo"),
etc.)
C# does not have Java-style non-static inner classes (which is fine with
me in general) so this forces the use of some other pattern (such as a
class which "switches" or several sub-classes) to get differrent
enumerators - all of which requires more code than necessary. (If it
weren't for the fact the just returning IEnumerable from the generator
works, whether it make sense or not...)


This is what C#2.0 custom iterators are for -- requires a bare minimum of
code.

class ManyIterators
{
private string[] values;

public IEnumerable<string> GetValues()
{
return this.values;
}

public IEnumerable<string> GetValuesInReverse()
{
for (int i=values.Length-1; i>=0; --i)
{
yield return this.values[i];
}
}

public IEnumerable<string> GetValuesAndAppend(string tail)
{
foreach (string s in this.values)
{
yield return s + tail;
}
}
}
ManyIterators iter;
foreach (string s in iter.GetValues())
{
//...
}
foreach (string s in iter.GetValuesInReverse())
{
//...
}

etc
Dec 4 '05 #6

"Ted Miller" <te*@millerzone.net> wrote in message
news:11*************@corp.supernews.com...
"Mike" <vi********@yahoo.com> wrote in message
news:Og**************@TK2MSFTNGP14.phx.gbl...
The key word there is "simply". The whole issue is that your class may
have multiple enumerators.
(For example, c.EnumInOrder(), c.EnumSorted(), c.EnumWithFilter("foo"),
etc.)
C# does not have Java-style non-static inner classes (which is fine with
me in general) so this forces the use of some other pattern (such as a
class which "switches" or several sub-classes) to get differrent
enumerators - all of which requires more code than necessary. (If it
weren't for the fact the just returning IEnumerable from the generator
works, whether it make sense or not...)
This is what C#2.0 custom iterators are for -- requires a bare minimum of
code.


Yes, I know.

I was pointing out that IEnumerator should be the required signature for
"foreach" to work - not IEnumerable.
The fact that the auto-generated code for the yeilding function in effect
generates an IEnumerable implementation that points to itself is what makes
this work, but is hardly necessary.

m

class ManyIterators
{
private string[] values;

public IEnumerable<string> GetValues()
{
return this.values;
}

public IEnumerable<string> GetValuesInReverse()
{
for (int i=values.Length-1; i>=0; --i)
{
yield return this.values[i];
}
}

public IEnumerable<string> GetValuesAndAppend(string tail)
{
foreach (string s in this.values)
{
yield return s + tail;
}
}
}
ManyIterators iter;
foreach (string s in iter.GetValues())
{
//...
}
foreach (string s in iter.GetValuesInReverse())
{
//...
}

etc

Dec 5 '05 #7

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

Similar topics

20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
21
by: Andreas Huber | last post by:
Hi there Spending half an hour searching through the archive I haven't found a rationale for the following behavior. using System; // note the missing Flags attribute enum Color {
18
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! I have created an enum list like this: enum myEnum : int { This = 2, That, NewVal = 10, LastItm
3
by: M | last post by:
I can pass in a string to a Web User Control, but I haven't been successful passing in an enum. I declared my enum as a public variable in my Web User Control itself. Thanks in advance.
1
by: Rich | last post by:
I have a solution that is in development under VS2003 and has been 'ported' to VS2005. The painful experience of running this newer version of VS revolves around one form. This form is very dense...
7
by: Dino Buljubasic | last post by:
I am having a problem when trying to view a form in desing view. If a form has any error or warning, the design view will not open the form. Anybody knows how to fix this error? Thank you...
1
by: John Olbert | last post by:
We are trying to use legacy Visual Basic 6 ActiveX controls in C# projects under Vs2005. The importing into Vs2005 Tools of the registered Ocx works fine. We can place an instance of the control on...
0
by: SenthilVel | last post by:
Hi All I have problems when i convert my 1.1 ASPX web application to 2.0 using VS2005. My web application gets compiled fine in CLR1.1 and there is no problem with that. When i do the...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.