Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Boo Primer: [Part 13] Enumerations

rollynoel edited this page · 3 revisions
Clone this wiki locally

Added by Cameron Kenneth Knight

info Definition: Enumeration - A set of name to integer value associations.

Declaring an Enumeration

Enumerations are handy to use as fields and properties in classes.

declaring an enum

enum Day:
    Sunday
    Monday
    Tuesday
    Wednesday
    Thursday
    Friday
    Saturday

class Action:
    [Property(Day)]
    _day as Day

Enumerations are also handy in preventing "magic numbers", which can cause unreadable code.

info Definition: Magic Number - Any number outside of -1, 0, 1, or 2.

Enumerations technically assign an integer value to each value, but that should generally be abstracted from view.

enum Test:
    Alpha
    Bravo
    Charlie

// is the same as

enum Test:
    Alpha = 0
    Bravo = 1
    Charlie = 2

recommendationRecommendation: Except in special cases, do not assign numbers.

Exercises

  1. Think of another good instance of using enums.

Go on to Part 14 - Exceptions

Back to: Home | Boo Primer

Something went wrong with that request. Please try again.