How to Properly Use Enums With Multiple Values

Java enum with multiple value types

First, the enum methods shouldn't be in all caps. They are methods just like other methods, with the same naming convention.

Second, what you are doing is not the best possible way to set up your enum. Instead of using an array of values for the values, you should use separate variables for each value. You can then implement the constructor like you would any other class.

Here's how you should do it with all the suggestions above:

public enum States {
...
MASSACHUSETTS("Massachusetts", "MA", true),
MICHIGAN ("Michigan", "MI", false),
...; // all 50 of those

private final String full;
private final String abbr;
private final boolean originalColony;

private States(String full, String abbr, boolean originalColony) {
this.full = full;
this.abbr = abbr;
this.originalColony = originalColony;
}

public String getFullName() {
return full;
}

public String getAbbreviatedName() {
return abbr;
}

public boolean isOriginalColony(){
return originalColony;
}
}

Check enum for multiple values

I ended up writing a method:

public static enum FileType {
CSV, XML, XLS, TXT, FIXED_LENGTH;

// Java < 8
public boolean in(FileType... fileTypes) {
for(FileType fileType : fileTypes) {
if(this == fileType) {
return true;
}
}

return false;
}

// Java 8
public boolean in(FileType... fileTypes) {
return Arrays.stream(fileTypes).anyMatch(fileType -> fileType == this);
}
}

And then:

if(fileType.in(FileType.CSV, FileType.TXT, FileType.FIXED_LENGTH)) {}

Nice and clean!

Is there a way to store multiple values in an enum entry?

That's not possible (unless you settle for somehow encoding you coordinates into a single integer, which, in my opinion, is an abuse of enum).

I suggest using a structure instead:

struct fvec2 // "fvec2" = a 2D vector of floats
{
float x, y;
};

const fvec2 city = {1,2}, town = {1,2};

Typescript enum with multiple string values

So Instead of creating an enum, just create an object in this format.

export const RewardCategory = { 
swapPoints: {variable: 'swapPoints', display: 'Swap Points' },
charity: {variable: 'charity', display: 'Charity' },
discountVouchers: {variable: 'discountVouchers', display: 'Discount Vouchers' }
}

Then, simply use it like this.

RewardCategory.swapPoints.display 

or

 RewardCategory.swapPoints.variable

How do you pass multiple enum values in C#?

When you define the enum, just attribute it with [Flags], set values to powers of two, and it will work this way.

Nothing else changes, other than passing multiple values into a function.

For example:

[Flags]
enum DaysOfWeek
{
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64
}

public void RunOnDays(DaysOfWeek days)
{
bool isTuesdaySet = (days & DaysOfWeek.Tuesday) == DaysOfWeek.Tuesday;

if (isTuesdaySet)
//...
// Do your work here..
}

public void CallMethodWithTuesdayAndThursday()
{
this.RunOnDays(DaysOfWeek.Tuesday | DaysOfWeek.Thursday);
}

For more details, see MSDN's documentation on Enumeration Types.


Edit in response to additions to question.

You won't be able to use that enum as is, unless you wanted to do something like pass it as an array/collection/params array. That would let you pass multiple values. The flags syntax requires the Enum to be specified as flags (or to bastardize the language in a way that's its not designed).

C++ Multiple enum elements being passed as parameter?

That is just a bitwise OR, which allows to combine more than one enum value into the integer which is is, internally (so still just one argument in the function call). Such enums are also called a flagset. They are usually characterized by members that have values which represent just one bit: a = 1, b = 2, c = 4, d = 8, e = 16, ... so they can be combined; a | b is 3 when casted to int.
Inside the function body, individual flags are then queried separately using the bitwise & operator.

additional reference: How to use enums as flags in C++?

btw. this concept seems to be quite similar in Java: Implementing a bitfield using java enums



Related Topics



Leave a reply



Submit