chrisdottel
chrisdottel

Reputation: 1133

How do I access enum values in Kotlin

I'm trying to iterate over an enum but I cannot access any values they were set with. Android Studio only sees the name and ordinal, but nothing else.

I'm not sure if this is an android studio problem as other online tutorials for kotlin enums seem to do what I do and work just fine.

    enum class TimeStamps(value : Long, text : String) {
        Hour(0, "Past Hour"),
        Day(3600000, "Today"),
        Yesterday(86400000, "Yesterday"),
        Week(172800000, "This week"),
        LastWeek(604800000, "Last week"),
        LastMonth(1209600000, "Last month"),
        LastYear(2628000000, "Last year"),
        LongTime(31540000000, "A long time ago")
    }


TimeStamps.LastMonth.value
TimeStamps.LastMonth.text  //Both of these are said to be undefined by Android Studio

enumValues<TimeStamps>().forEach {

            it.value //Also undefined
}

I'm not exactly sure what is going on. The only error message I get is the Unresolved Reference error. Any help is greatly appreciated, thanks!

Upvotes: 5

Views: 4945

Answers (1)

TheOperator
TheOperator

Reputation: 6476

You need to use val if you want it to be a property:

enum class TimeStamps(val value: Long, val text: String) {
    ...
}

Upvotes: 18

Related Questions