Home > database >  How to Store Class in a Variable Kotlin
How to Store Class in a Variable Kotlin

Time:12-19

i want to store a Class in a variable, the purpose is to check if other variable is an instanceOf the class
Here is My Code :

            when (it.itemId) {
                R.id.vend_list -> {
                    replace(R.id.fragmentContainer, vendingList)
                    id = VendListClass
                }
                R.id.label -> {
                    replace(R.id.fragmentContainer, label)
                    id = LabelClass
                }
                R.id.home -> {
                    replace(R.id.fragmentContainer, mainMenu)
                    id = MainMenuClass
                }
                R.id.statistic -> {
                    replace(R.id.fragmentContainer, statistic)
                    id = StatisticClass
                }
                else -> {}
            }

            for(fragment in supportFragmentManager.fragments){
                if(fragment !is id){
                    remove(fragment)
                }
            }

CodePudding user response:

I don't know your exact requirement. But it probably could be designed in other ways, enum? sealed classes? inheritances?

Anyway, straight to your question, hope this helps:

val listCls = List::class
val strCls = String::class

val listObj = listOf(1,2,3)

println("check listObj and listCls: ${listCls.isInstance(listObj)}")
println("check listObj and strCls: ${strCls.isInstance(listObj)}")

output:

check listObj and listCls: true
check listObj and strCls: false

CodePudding user response:

Thanks @Kent for helping, but i figured out how to solve this in an Ineffective way
Here is My Code :

val obj1 = Object1()
val obj2 = Object2()
val obj3 = Object3()
val obj4 = Object4()

var id : SuperObject? = null

when(certainConditions) {
   option1 -> id = Object1()
   option2 -> id = Object2()
   option3 -> id = Object3()
   option4 -> id = Object4()
}

val otherObject = Object1()

if(id == otherObject) {
    //process logic
}

im still looking for the more effective way tho.
If anyone found out a better way, Please share your answer.

CodePudding user response:

You can store a class reference in a KClass<*> variable using ::class or in a Class<*> variable using ::class.java

So based on your original code, this is how you could do it

            // this has to be a nullable type because of your else option
            var id: KClass<*>? = null
    
            when (it.itemId) {
                R.id.vend_list -> {
                    replace(R.id.fragmentContainer, vendingList)
                    id = VendListClass::class
                }
                R.id.label -> {
                    replace(R.id.fragmentContainer, label)
                    id = LabelClass::class
                }
                R.id.home -> {
                    replace(R.id.fragmentContainer, mainMenu)
                    id = MainMenuClass::class
                }
                R.id.statistic -> {
                    replace(R.id.fragmentContainer, statistic)
                    id = StatisticClass::class
                }
                else -> {}
            }

            for(fragment in supportFragmentManager.fragments){
                // if the else option from when has to remove all
                // fragments, just flip the condition to
                // if(id == null || id.isInstance(fragment))
                if(id != null && id.isInstance(fragment)){
                    remove(fragment)
                }
            }
  • Related