2

I have a scenario. I have created a data class in Kotlin like this:

data class AgentDetails(
        val mobileNo: String,
        val Name: String,
        val Email: String,
        val Password: String,
        val Occupation: String,
        val pincode: String,
        val usertype: String,
        val profilepic: String,
        val AccountStatus: String
)

I want to send different type of objects of this data class to a web service:

1st object example:

val agentDetails = AgentDetails(mobileNo = mobileNumberText.text.toString(),
                        Name = userNameText.text.toString(),
                        Email = emailIdText.text.toString(),
                        Password = HashUtils.sha1(passwordText.text.toString()),
                        Occupation = item,
                        pincode = pinCodeText.text.toString(),
                        usertype = "Agent",
                        profilepic = "null", AccountStatus = "pending")

In 2nd object I only want to send mobile number. I dont wanna include any other field. Something like this:

val agentDetails = AgentDetails(mobileNo = mobileNumberText.text.toString())

And in 3rd object I only wanna send email id. Instead of creating multiple data classes. Can I use the same data class for multiple implementations?

0

2 Answers 2

2

Personally, I'd define three objects because they represent three different concepts (or projections of a concept). But if you make your properties nullable and provide a default value of null, you can get away with creating them as you want...

data class AgentDetails(
    val mobileNo: String? = null,
    val name: String? = null,
    val email: String? = null,
    val password: String? = null,
    val occupation: String? = null,
    val pincode: String? = null,
    val usertype: String? = null,
    val profilepic: String? = null,
    val accountStatus: String? = null
)

Note: I've changed some of your property names to camelCase, as is the proper convention. And these all work fine:

AgentDetails(mobileNo = mobileNumberText.text.toString())
AgentDetails(email = "[email protected]")
AgentDetails(name = "Foo", password = "Bar")

All of the other fields not provided will be null, and the types will be nullable, so you'll have to guard against that. Otherwise, I'd define three data classes for this.

Another solution would be to consider a sealed class structure:

sealed class AgentDetails
data class AgentByName(val name: String) : AgentDetails()
data class AgentByEmail(val email: String): AgentDetails()
// etc..

And then use it in a when expression:

fun doSomethingWithAgents(agentDetails: AgentDetails) {
    when (agentDetails) {
        is AgentByName -> // Do something
        is AgentByEmail -> // Do Something
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

The easiest way is to make the fields nullable and provide default values:

data class AgentDetails(
    val mobileNo: String? = null,
    val Name: String? = null,
    val Email: String? = null,
    val Password: String? = null,
    val Occupation: String? = null,
    val pincode: String? = null,
    val usertype: String? = null,
    val profilepic: String? = null,
    val AccountStatus: String? = null
)

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.