DEV Community

loading...

Is there a way to make a new enum from an array of `String`s?

baenencalin profile image Calin Baenen ・1 min read

I know there's a way to convert a String[] to a list of <EnumType>[], but that's not what I want.

For ease of creation, I need a way to convert a list of tokens to a new enum.

E.g.

String[] strVals = {"MY_FIRST_CONSTANT", "MY_SECOND_CONSTANT"}
Emum ValCollection = /* Way to convert String[] to Enum. */
Enter fullscreen mode Exit fullscreen mode

Thanks!
Cheers!

Discussion (9)

pic
Editor guide
Collapse
redcreator37 profile image
RedCreator37

So if I understand correctly, you're trying to make an enum of entries with String values. Perhaps something like this:

I don't know what you want to do, but this is how I actually translated your example code....

package test;

/**
 * @author The Elite Gentleman
 *
 */
public enum Strings {
    STRING_ONE("ONE"),
    STRING_TWO("TWO")
    ;

    private final String text;

    /**
     * @param text
     */
    Strings(final String text) {
        this.text =
Collapse
baenencalin profile image
Calin Baenen Author

Yes, I am trying to make an enum of entries with string values.
Unfortunately, this won't work, as CSharp only allows numeric values in enums.

When you try to do this, you get: error CS0029: Cannot implicitly convert type 'string' to 'int'.
If I try to define the enum's type, I get: error CS1008: Type byte, sbyte, short, ushort, int, uint, long, or ulong expected.

You can refer to the docs here (Enumeration types - C# reference | Microsoft Docs).

Collapse
redcreator37 profile image
RedCreator37 • Edited

Apparently you can parse the enum value names into strings as shown in this example from the MSDN doc you've linked. You could then try extracting them and putting them into an array or something similar. There are some minor inconveniences involved (it'd be good to make a wrapper for it / add some helper methods).

Edit: just to clarify, the enum values still aren't of type string, but it's possible to indirectly use them as such.

Edit 2: have you considered using HashMaps or something similar instead? There's more overhead to them (an overkill for using them as constant enum values) but it might be what you're looking for if you'd like to dynamically insert data. I don't know in what context you're going to be using these so I can't say for sure.

Thread Thread
baenencalin profile image
Calin Baenen Author

I'm making a command-line interface, and I have a list of commands.

I want to use a list of command strings to generate an enum.
Why do this when you have the list?
Because this:

switch(cmd) {
    case COMMANDS[0]: /* ... */
}
Enter fullscreen mode Exit fullscreen mode

It throws an error that the case isn't constant (as I GUESS it should rightfully do so), therefore, I need to manually right all of the commands down again.

Thread Thread
redcreator37 profile image
RedCreator37

Ah, this makes sense. Since it won't let you use plain string arrays, you could try a more OOP approach using Dictionaries and custom command classes.

In the example, there's a dictionary containing all available commands. Each command implements the ICommand interface (and the Execute() method, which gets executed by the while loop in Main). You can add fields to the ICommand interface to store the command's name or usage description if you want.

This doesn't directly answer your question but hopefully helps in some way.

Thread Thread
baenencalin profile image
Calin Baenen Author

Doesn't making a new class for every command seem bloated?
Otherwise, this does help.

Thread Thread
baenencalin profile image
Calin Baenen Author

Thanks for the help.

Thread Thread
redcreator37 profile image
RedCreator37

Doesn't making a new class for every command seem bloated?
Otherwise, this does help.

It's actually advisable because it makes adding new functionality / commands easier and the code is more readable/easier to maintain. The performance impact is minimal (C# is really good at optimizations).

baenencalin profile image
Calin Baenen Author

Is there a way I can convert all the String constants of a class to a String[]?