Questions tagged [enumeration]

The process of enumerating values, for example from some collection.

Use enums for enumeration types.

1779 questions
123
votes
26 answers

Are booleans as method arguments unacceptable?

A colleague of mine states that booleans as method arguments are not acceptable. They shall be replaced by enumerations. At first I did not see any benefit, but he gave me an example. What's easier to understand? file.writeData( data, true…
Thomas Koschel
  • 3,283
  • 8
  • 31
  • 38
114
votes
6 answers

Java Enum Methods - return opposite direction enum

I would like to declare an enum Direction, that has a method that returns the opposite direction (the following is not syntactically correct, i.e, enums cannot be instantiated, but it illustrates my point). Is this possible in Java? Here is the…
Skyler
  • 2,704
  • 5
  • 19
  • 31
105
votes
2 answers

Get enumeration name by value

Are there any standard methods to get Enumeration names by value? An example: class Example(enum.Enum): one = 1 two = 2 ex_variable = 1 Given ex_variable, can I obtain the string contained in Example.one.name?
Jiloc
  • 2,568
  • 3
  • 20
  • 37
102
votes
11 answers

Is there a predefined enumeration for Month in the .NET library?

I'm looking to see if there is an official enumeration for months in the .net framework. It seems possible to me that there is one, because of how common the use of month is, and because there are other such enumerations in the .net framework. For…
Mark Rogers
  • 90,043
  • 18
  • 79
  • 129
100
votes
7 answers

(How) can I count the items in an enum?

This question came to my mind, when I had something like enum Folders {FA, FB, FC}; and wanted to create an array of containers for each folder: ContainerClass*m_containers[3]; .... m_containers[FA] = ...; // etc. (Using maps it's much more…
fuenfundachtzig
  • 6,344
  • 11
  • 54
  • 77
100
votes
7 answers

Singular or plural for enumerations?

Do you use singular or plural for enumerations? I think it makes best sense with plural in the declaration enum Weekdays { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } ... but I think it makes more…
Jan Aagaard
  • 10,130
  • 7
  • 40
  • 66
89
votes
10 answers

Intelligent way of removing items from a List while enumerating in C#

I have the classic case of trying to remove an item from a collection while enumerating it in a loop: List myIntCollection = new…
John Stock
  • 934
  • 1
  • 6
  • 9
87
votes
9 answers

Removing from array during enumeration in Swift?

I want to enumerate through an array in Swift, and remove certain items. I'm wondering if this is safe to do, and if not, how I'm supposed to achieve this. Currently, I'd be doing this: for (index, aString: String) in enumerate(array) { //Some…
Andrew
  • 6,993
  • 10
  • 39
  • 72
87
votes
1 answer

What is the BOOL *stop argument for enumerateObjectsUsingBlock: used for?

I've been using enumerateObjectsUsingBlock: a lot lately for my fast-enumeration needs, and I'm having a hard time understanding the usage of BOOL *stop in the enumeration block. The NSArray class reference states stop: A reference to a Boolean…
Mick MacCallum
  • 124,539
  • 40
  • 277
  • 276
86
votes
10 answers

Iterate enum values using java generics

I'm trying to find a way to iterate through an enum's values while using generics. Not sure how to do this or if it is possible. The following code illustrates what I want to do. Note that the code T.values() is not valid in the following…
Tauren
  • 25,387
  • 37
  • 126
  • 165
85
votes
11 answers

What is the best way to modify a list in a 'foreach' loop?

A new feature in C# / .NET 4.0 is that you can change your enumerable in a foreach without getting the exception. See Paul Jackson's blog entry An Interesting Side-Effect of Concurrency: Removing Items from a Collection While Enumerating for…
Polo
  • 1,562
  • 3
  • 16
  • 29
80
votes
16 answers

Enumerate or list all variables in a program of [your favorite language here]

A friend asked me last week how to enumerate or list all variables within a program/function/etc. for the purposes of debugging (essentially getting a snapshot of everything so you can see what variables are set to, or if they are set at all). I…
Kurt
  • 4,034
  • 4
  • 21
  • 29
80
votes
9 answers

Collection was modified; enumeration operation may not execute in ArrayList

I'm trying to remove an item from an ArrayList and I get this Exception: Collection was modified; enumeration operation may not execute. Any ideas?
Ricardo
  • 10,019
  • 8
  • 23
  • 37
79
votes
7 answers

The foreach identifier and closures

In the two following snippets, is the first one safe or must you do the second one? By safe I mean is each thread guaranteed to call the method on the Foo from the same loop iteration in which the thread was created? Or must you copy the reference…
xyz
  • 25,370
  • 28
  • 99
  • 125
78
votes
9 answers

Distinction between iterator and enumerator

An interview question for a .NET 3.5 job is "What is the difference between an iterator and an enumerator"? This is a core distinction to make, what with LINQ, etc. Anyway, what is the difference? I can't seem to find a solid definition on the net.…
GurdeepS
  • 58,809
  • 95
  • 236
  • 371