1

Scenario/Problem

In the below code example, I implemented IMessage from java to kotlin Message. But when I pass a value to message variable it says val cannot be reassigned

val message = Message()
message.user = Author()
message.text = ""
messate.createdAt = Date()
message.id = ""

public interface IMessage {
    String getId();
    String getText();
    IUser getUser();
    Date getCreatedAt();
}

class Message : IMessage {
    private var id: String = ""
    private var createdAt = Date()
    private var user = Author()
    private var text: String = ""

    override fun getId(): String {
        return id
    }

    override fun getCreatedAt(): Date {
        return createdAt
    }

    override fun getUser(): IUser {
        return user
    }

    override fun getText(): String {
        return text
    }

}

Question

  1. What part of the code is wrong?

Expected Result

 val message = Message()
 message.id = ""
 message.text = ""
 message.user = Author()

2 Answers 2

2

Please change user object to IUser as I am not sure what it contains.

Interface

public interface IMessage {

    String getId();

    void setId(@NonNull String id);

    String getText();

    void setText(@NonNull String text);

    String getUser();

    void setUser(@NonNull String user);

    Date getCreatedAt();

    void setCreatedAt(@NonNull Date createdAt);
}

Message Class

class Message : IMessage {
    override fun setId(id: String) {
        this.id = id
    }

    override fun setText(text: String) {
        this.text = text
    }

    override fun setUser(user: String) {
        this.user = user
    }

    override fun setCreatedAt(createdAt: Date) {
        this.createdAt = createdAt
    }


    override fun getId(): String {
        return id
    }

    override fun getText(): String {
        return text
    }

    override fun getUser(): String {
        return user
    }

    override fun getCreatedAt(): Date {
        return createdAt
    }

    override fun toString(): String {
        return StringBuilder().append(id).append(user).append(text).append(createdAt.toString()).toString()
    }

    private var id: String = "id"
    private var createdAt: Date = Date()
    private var user: String = "user"
    private var text: String = "text"
}

MainActivity Code onCreate

        val message = Message()
        message.user = "Ganesh"
        message.createdAt = Date()
        message.id = "1"
        message.text = "Hello World"

        Log.e("First message object ", message.toString())

        message.user = "Tikone"
        message.createdAt = Date()
        message.id = "2"
        message.text = "Hello Kotlin"

        Log.e("After message object ", message.toString())

Result

09-11 12:30:40.188 29575-29575/com.sysanenet.myapplication E/First message object: 1GaneshHello WorldTue Sep 11 12:30:40 GMT+05:30 2018

09-11 12:30:40.188 29575-29575/com.sysanenet.myapplication E/After message object: 2TikoneHello KotlinTue Sep 11 12:30:40 GMT+05:30 2018

Sign up to request clarification or add additional context in comments.

4 Comments

in this example, I'm still having Val cannot be reassigned var message = Message() message.user = Author() message.text = "" messate.createdAt = Date() message.id = ""
Please check, this is working. Interface implemented by your compiler was treating it as getter and setter method. In previous code we only had a getter. hence you can not access it with dot operator and change value.
IMessage is from library, so I cannot change the current code
IMessage can be used for getting data from Message class. You can not set data to Message object then.
1

In the below code is my answer, but I think I broke the rule of kotlin getter setter, because I created setter..

class Message : IMessage {
    private var id: String? = null
    private var text: String? = null
    private var user: IUser? = null
    private var createdAt: Date? = null

    fun setId(id: String) {
        this.id = id
    }

    fun setText(text: String) {
        this.text = text
    }

    fun setUser(user: IUser) {
        this.user = user
    }

    fun setCreatedAt(createdAt: Date) {
        this.createdAt = createdAt
    }

    override fun getId(): String? {
        return id
    }

    override fun getText(): String? {
        return text
    }

    override fun getUser(): IUser? {
        return user
    }

    override fun getCreatedAt(): Date? {
        return createdAt
    }

}

My Result

 val message = Message()
 message.setId("")
 message.setText("")
 message.setUser(Author())

Comments

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.