Home > Mobile >  calling a double from object seems to return the possition not value of the double
calling a double from object seems to return the possition not value of the double

Time:02-13

In a function i have this:

val sunRise = SunEquation(2459622)
binding.timeDisplay.setText("$sunRise.n")

The SunEquation-Class looks like this:

class SunEquation(var jDate: Int,) {

    val jYear = 2451545
    val ttOffset = .0008

    var n = jDate - jYear   ttOffset
}

the button- text that appears is:

[email protected]

i would expect a double-value

CodePudding user response:

You have to add curly brackets around the value you want to inject into the String, like this:

binding.timeDisplay.setText("${sunRise.n}")

The shorthand syntax without brackets only works for a single variable, but not for access to a nested field or other more complex expressions.

In your case, this results in the object itself being injected into the String, which is resembled by com.example.soluna.SunEquation@6d1a94b based on the result of the corresponding toString() call, which defaults to the class name and the reference id of the object. Followed by the literal String .n.

Alternatively, you could extract the value into a val beforehand and reference that.

val customN = sunRise.n
binding.timeDisplay.setText("$customN")
  • Related