Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

com.example.soluna.SunEquation@6d1a94b.n

i would expect a double-value

>Solution :

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

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

The shorthand syntax without paranthesis 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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading