Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
haXe 2 Beginner's Guide

You're reading from  haXe 2 Beginner's Guide

Product type Book
Published in Jul 2011
Publisher
ISBN-13 9781849512565
Pages 288 pages
Edition 1st Edition
Languages

Table of Contents (21) Chapters

haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Getting to know haXe 2. Basic Syntax and Branching 3. Being Cross-platform with haXe 4. Understanding Types 5. The Dynamic Type and Properties 6. Using and Writing Interfaces, Typedefs, and Enums 7. Communication Between haXe Programs 8. Accessing Databases 9. Templating 10. Interfacing with the Target Platform 11. A Dynamic Website Using JavaScript 12. Creating a Game with haXe and Flash Pop Quiz Answers Index

Time for action – Switching over enums with parameters


Switching over an enum that has parameters is done in the same way as switching over a basic enum. The only difference is that we will have to write the names of the parameters in order to get them.

Imagine that we have an enum representing a color either as RGB or RGBA and that we want to get the string representing this color.

  1. First, let's write our enum:

    enum Color
    {
       rgb(r : Int, g : Int, b : Int);
       rgba(r : Int, g : Int, b : Int, a : Int);
    }
  2. Now, let's write our switch:

       public static function fromColorToString(color :Color)
       {
          switch(color)
          {
             case rgb(r, g, b):
                return "RGB(" + r + "," + g + ", " + b + ")";
             case rgba(r, g, b, a):
                return "RGB(" + r + "," + g + ", " + b + "," + a + ")";
          }
       }

What just happened?

In the second step, we got parameters from the color so that we could construct the string.

As you can see, you don't need to rewrite the types of the parameters...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at ₹800/month. Cancel anytime}