Name of the variable in looping

Asked

Viewed 16 times

-4

I need to display the name of the variable in which it contains the value that is inside the range... in case the variable name 'value2'.

Is there a way to find the name of this variable for display instead of the value contained in it?

Show which variable is in the range.

fun verificaRange(valor1: Int, valor2: Int, valor3: Int){
    var valores = intArrayOf(valor1, valor2, valor3)
    for (valor in valores){
        if (valor in 100..1000){ // Verificar se alguma delas é maior do que 1000 ou menor do que 100...
            println("A ${::valores[valor].name} está no range!")} // Tentei algo assim, mas sem sucesso!
        else{
            println("Nenhuma outra variável está dentro do range.")
        }
    }
}

fun main(){
    var valor1: Int = 99
    var valor2: Int = 100
    var valor3: Int = 10001
    verificaRange(valor1, valor2, valor3)
}
  • 1

    It is probably a XY problem (I suggest reading and understanding before editing this post, can make all the difference). You can [Dit] the post and try to explain the ultimate goal you want instead of the way you think the solution is, that automatically the post enters a revaluation queue and may eventually be reopened.

  • The way you want it there is no possibility because like Java, Kotlin passes arguments of primitive type by value and here verificaRange(valor1, valor2, valor3) when compiling the code generated by the JVM replaces the variables with their values.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.