Juce Strings and Enums

Hi all,

Does anyone know how I would go about using a Juce String to return the value of an Enum?

enum HitType{
        Kick = 0,
        Snare,
        Hat,
        Ride,
        Crash,
        TotalHitTypes
    };

/===============

 HitClassButtons.add(new ToggleButton("Kick"));
        HitClassButtons.add(new ToggleButton("Snare"));
        HitClassButtons.add(new ToggleButton("Hat"));
        HitClassButtons.add(new ToggleButton("Ride"));
        HitClassButtons.add(new ToggleButton("Crash"));

/==============


void buttonStateChanged (Button*)
    {
        
        int type = HitType::Button.getName(); //Obviously wrong but what is the correct syntax?
    }

There’s no such direct conversion possible. You have to manually go through the strings with an “if/else if” setup.

Ok, I’ll do that then. Cheers!

Build a StringArray you’re populating with your enum items, like this "StringArray hitTypeStr(“Kick\nSnare\n…etc…”);"
Then you can use “int type = hitTypeStr.indexOf(button.name());”

As a side benefit, you’ll be able to loop through your array for constructing your buttons, instead of manually copy & pasting code.

1 Like

That’s perfect X-Ryl669. I had seen that done in other peoples code I just couldn’t recall exactly how. Thanks!