https://kotlinlang.org logo
Title
d

diesieben07

06/22/2017, 9:40 AM
Only the constants you define are the values of the enum.
j

jasonlow

06/22/2017, 9:43 AM
diesieben07: So, by constants, you mean the “Bill” in
BILL("Bill")
?
If so, given my previous code, I should be doing something like:
foo.valueOf("Bill")
?
this gives me an exception as well
d

diesieben07

06/22/2017, 9:50 AM
No, i mean the
BILL
and
TOP_UP
.
"Bill"
is just a value of the
transactionType
field in the enum constant object. The name of your enum constants (used with
valueOf
) is
"BILL"
and
"TOP_UP"
in your case.
If you want to look up an enum object by the
transactionType
field you would use
foo.values().firstOrNull { it.transactionType == "bill" }
1
j

jasonlow

06/22/2017, 10:00 AM
That is perfect, thank you @diesieben07 !
k

karelpeeters

06/22/2017, 11:02 AM
Or is you know the name of the enum value, there's also a "valueOf" method, so in your case
foo.valueOf("BILL")
would have worked too.