0
data class Thermostat(
    // ... other properties

    @SerializedName("units")
    private var _units: String?, // Fahrenheit,

    // ... other properties
) : Serializable {
    var units: String? = null
        get() = _units
        set(value) {
            MyApplication.temperatureServerIsCelsius = (!value?.equals("Fahrenheit")!!)
            _units = value
        }
}

When _units receives the value, I want to assign that value to units (inside the model class) but, it isn't doing so here.

Actually, I want a variable in MyApplication modified in accordance with the value of the _units field.

How can I achieve that? Is there any way we can use get set method directly in field? like this(I know these is not correct but anything like these possible?)

data class Thermostat(
    // ... other properties

    @SerializedName("units")
    private var _units: String?
       get() = _units
       set(value) {
                MyApplication.temperatureServerIsCelsius = 
                (!value?.equals("Fahrenheit")!!)
            }
    )

Any solution or any other way to do this type thing?.

2 Answers 2

0

You need to remove the default initializer from the unit.

data class Thermostat(
    private var _units: String?, // Fahrenheit,
) {
    var units: String?
        get() = _units
        set(value) {
            _units = value
        }
}

Usage :

 val s = Thermostat(null)
    s.units = "Fahrenheit"
    println(s)

Output :

Thermostat(_units=Fahrenheit)
Sign up to request clarification or add additional context in comments.

10 Comments

thx! but still not working. I want assigning value in set block to a variable in application class but it is not assigned >>>>set(value) { MyApplications.testVariable= _units.toString() _units = value }
Actually I want to check the _units value and apply condition on that basis in that set block inside the data class.
I have made a demo here, take a look pl.kotl.in/haAhc5rmu
Thanks for your effort. But in Android studio it tell this with red line "value-parameter val get: [Error type: Missed a type for a value parameter get]"
There is no val in the link I have just sent. It is working properly in android studio as well.
|
0

I suggest to avoid the idea of setting fields after creating instance of the data class. The point of data classes is to consistently contain data, and immutable data is usually safer than mutable.

I don't think that there is a point to store private data in a data class either (at least in your case), because data classes should not have logic. Your set method has side-effect, which is not a good practice. You should avoid side-logic in setters, do it separately. Nobody expects set method to do something else besides setting the value.

The easiest way to have an instance of the data class with up-to-date data is to create one in place.

3 Comments

👍👍Okay. I was just doing this as I am getting the value in particular units and need to convert all that in another unit.
@MohammadTaqi you should do this outside of the data class
Yes. I believe I only need to accomplish that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.