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

Really, how do you make enumerations?

I had VS (2008) telling me my class isn't enumerable when trying to use a foreach loop. This happens quite often, and every time I Google around for an hour trying to find a solution that applies to my case. It needs to be understandable and easy to implement. It needs to work with List<Tcollections. It should be the best practice in .NET 3.5. Can anyone point me once and for all to the right solution? The best I can find right now is this:

http://msdn.microsoft.com/en-us/libr...numerable.aspx

But I don't want to write all that every time I make a collection lass, and I don't like the public class variable in the enumerator class. Didn't enumerations get a lot easier in .NET 2.0?

Gustaf
Sep 24 '08 #1
2 1066
On Sep 24, 11:40*am, Gustaf <gust...@algonet.sewrote:

<snip>
But I don't want to write all that every time I make a collection lass,
and I don't like the public class variable in the enumerator class.
Didn't enumerations get a lot easier in .NET 2.0?
Implementing IEnumerable and IEnumerator (and the generic equivalents)
became a lot easier in C# 2 with iterator blocks (method
implementations using "yield return" and "yield break" statements).

See the following links for a couple of articles and a free chapter
from my book (C# in Depth) which covers iterator blocks:
http://csharpindepth.com/Articles/Ch...Iterators.aspx
http://csharpindepth.com/Articles/Ch...mentation.aspx
http://www.manning-source.com/books/...ter6sample.pdf

Jon

Sep 24 '08 #2
t needs to work with List<Tcollections.
Can you clarify what you have already? If you have a class that is
encapsulating a single list, then you can simply forward the list's
enumerator:

class Test : IEnumerable<string>
{
private List<stringinnerList = new List<string>();

public IEnumerator<stringGetEnumerator()
{
return innerList.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}

If you have multiple lists, you can use an iterator block to flatten
them:

class Test : IEnumerable<string>
{
private List<stringlistA = new List<string>(),
listB = new List<string>();

public IEnumerator<stringGetEnumerator()
{
foreach (string s in listA) {yield return s;}
foreach (string s in listB) { yield return s; }
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}

If that doesn't help, can you clarify what you have?

Marc
Sep 24 '08 #3

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

Similar topics

1
by: Joyce | last post by:
In my schema I have 2 enumerations, let's say, country description and country code, and I want to use them so I can map each country description to its precise country code (and no other). So far...
21
by: Christopher Benson-Manica | last post by:
I'll try to explain what I want to do: I have foo.h and foo.cpp. Units that include foo.h will define an enumeration bar: enum bar { typeNone, typeBaz, typeQuux, ... , count }; A method...
3
by: JoeH | last post by:
Hi, I'm using a COM DLL (created in VB) in my javascript code and can successfully call its methods and get/set its properties. There are also some Public enumerations defined in the ActiveX...
5
by: Seamus M | last post by:
I can't find any info on enumerations in the PHP manual, so I assume there is no built in way to create them. Can anyone tell me the best way to build a simple enumeration, such as: Enum...
2
by: Glenn | last post by:
I've been trying to generate enumerations with specific integer values using an XSD and xsd.exe. e.g public enum AccessSecurity { Normal = 1, Restricted = 100001 }
1
by: Oleg Ogurok | last post by:
Hi all, I've added a new DataSet (xsd file) to my project in VS.NET 2003. There I create a simple type as an enumeration of values. <xs:simpleType name="MyCustomType"> <xs:restriction...
4
by: ChrisB | last post by:
Hello: I will be creating 50+ enumerations related to a large number of classes that span a number of namespaces. I was wondering if there are any "best practices" when defining enumerations. ...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
77
by: Ben Finney | last post by:
Howdy all, PEP 354: Enumerations in Python has been accepted as a draft PEP. The current version can be viewed online: <URL:http://www.python.org/peps/pep-0354.html> Here is the...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.