Programming in Dart: Classes

Jun 28 2022 · Dart 2.17, Flutter 3.0, DartPad

Part 1: Understand Classes

09. Utilize Enumerations

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 08. Create Static Members Next episode: 10. Conclusion

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Before moving forward into the next and final part of this course, we’re going to explore another type that will help clarify your code. They are called enumerations. Often times in programming, you will hear the expression, ‘avoid magic numbers’. Take a look at this code. Here we are checking that the current item is greater than eighteen.

var damageModifiers = [1, 2, 4];
var currentDamageModifier = 0;
var baseDamage = 100;
enum DamageType { regular, twice, quad }
 var currentDamageType = DamageType.twice;
switch (currentDamageType) {
    case DamageType.regular:
        currentDamageModifier = damageModifiers[0];
        break;
    case DamageType.twice:
        currentDamageModifier = damageModifiers[1];
        break;
    case DamageType.quad: 
        currentDamageModifier = damageModifiers[2];
        break;
}
print('You do ${baseDamage * currentDamageModifier } damage');
/**
  switch (currentDamageType) {
    case DamageType.regular:
      damageModifier = damageValues[0];
      break;
    case DamageType.twice:
      damageModifier = damageValues[1];
      break;
    case DamageType.quad: 
      damageModifier = damageValues[2];
      break;
  }
**/
currentDamageModifier = damageModifiers[currentDamageType.index];