Skip to main content

Timeline for Loops for enumerated types

Current License: CC BY-SA 4.0

20 events
when toggle format what by license comment
Dec 1, 2021 at 0:15 comment added T. Sar @Flater at least for Vb.Net, there is this. Way less hackish than the proposed solution on the OP.
Nov 30, 2021 at 20:36 comment added T. Sar ...Seriously, for some reason it is extremely upsetting to me that this... hack was published in a book as something serious that you should do. Argh, this is horrible!
Nov 30, 2021 at 19:46 comment added T. Sar For C# at least, use this instead of the solution proposed in the book. It's far easier on the mind and prevents strange mistakes - all without corrupting your enum types.
Nov 30, 2021 at 19:39 comment added T. Sar Honestly, this particular example is terrible advice. "Country_Last" is a valid value for this enum but it what it means can change down the line just because you added another value to the list. This breaks the entire reason of using enums in the first place.
Nov 30, 2021 at 19:38 comment added T. Sar "Why do we need duplicated indices on the edges for enumerated type?" - Because iterating like this on enums is very weird and not really idiomatic, and you have to create this hack-ish aberration to make it work. There are better ways of doing this with minimal extra scaffolding that don't result in such a terrible headache for your future you five weeks down the line.
S Nov 30, 2021 at 12:48 history suggested jonrsharpe CC BY-SA 4.0
Include the quoted section
Nov 30, 2021 at 10:40 comment added Flater @jonrsharpe: Haven't read it so I can't comment on the reasoning, but I personally dislike the notion that you should rely on continuity here. The integer values of the enum should not be of concern to the enum's consumer, and could be completely arbitrary for all the consumer cares. The posted answer is the correct one, but I seriously dislike the allround approach there. Unless VB does not have another way to enumerate its enum values - at which point I'd still call it a dirty workaround but at least one in absence of a good way of doing things.
Nov 30, 2021 at 10:36 comment added jonrsharpe @Flater the risks of discontinuous enumerations (e.g. 1, 2, 4, 8 for bit flags) are also covered in that section!
Nov 30, 2021 at 10:32 comment added Flater @EugZ "Why indices" Tangentially: that integer value is not an index. They do not have to be continuous, nor do they have to be 0- or 1-indexed. You can set these values to mimic those of an index, but that's just your choice to do so then.
Nov 30, 2021 at 10:29 comment added Flater @EugZ: There are duplicate values because there are duplicate meanings. Referring to USA and referring to "the last value" are two different concepts that may currently overlap (due to USA being the last value) but could one day diverge (when another country is added).
Nov 30, 2021 at 10:27 comment added jonrsharpe Again, for one thing, "because iterating from first to last is more intent revealing than iterating from China to the USA".
Nov 30, 2021 at 10:26 review Suggested edits
S Nov 30, 2021 at 12:48
Nov 30, 2021 at 10:23 comment added CoderDesu Ok, nice. Why indices should be duplicated?
Nov 30, 2021 at 10:22 comment added jonrsharpe Indeed finding a PDF it explicitly tells you that this "... allows you to write a loop that loops through the elements of an enumeration... [these] values can be used as loop limits" with the VB example For iCountry = Country_First To Country_Last.
Nov 30, 2021 at 10:21 vote accept CoderDesu
Nov 30, 2021 at 8:48 review Close votes
Dec 5, 2021 at 3:02
Nov 30, 2021 at 8:34 answer added Philip Kendall timeline score: 7
Nov 30, 2021 at 8:14 comment added jonrsharpe I'd imagine a nearby paragraph explains just that, but probably because iterating from first to last is more intent revealing than iterating from China to the USA.
Nov 30, 2021 at 8:05 history edited CoderDesu
edited tags
Nov 30, 2021 at 7:35 history asked CoderDesu CC BY-SA 4.0