1
votes

I have a json response like below:

 {    
 "usersList": [
    {
        "fullname": "Sreejith",
             "email": null,
        "phone": null
     },
     {
        "fullname": "",
             "email": "[email protected]",
        "phone": null
     },
     {
       "fullname": "",
             "email": null,
        "phone": "8956231245"
     },
 ]
}

Currenntly I am binding the fullname value like below:

    <Label 
        Text="{Binding fullname}"
        Font="Bold,17" 
        TextColor="#676767"
        HorizontalOptions="Start" 
        VerticalOptions="Center"/>

But in some cases the fullname value is empty, so in that case I want to set the email value to label, also if email is null need to set the phone value. First preference is for fullname, then for email last for phone.

I created a new property like below, but getting an error for get: Error is not all code paths return a value.

 public string NameToVisualize { get { if (fullname != null) return fullname;
            else if (email != null) return email;
            else if (phone1 != null) return phone1;  } }

Is this method work?

1
How about creating a new property "DisplayName" in your entity class (I think it's "User" here) and with getter only. This property will return the name value according to your preference. The issue with this solution is it's for one-way binding ONLY (from source to target).Yang You
@yyou Edited the question with new property code, can you please checkSreejith Sree
Did you read the error, though? It says that not all code paths return a value. You do not return anything if fullname, email and phone1 is null. You need another return statement for this case. Also, your fullname is not null, it's an empty string.Marcel Kirchhoff

1 Answers

1
votes

Well your answer lies in your question itself actually this is how you do it

   public string NameToVisualize
    {
        get
        {
            if (!string.IsNullOrEmpty(Fullname) && !string.IsNullOrWhiteSpace(Fullname))
                return Fullname;
            else if (!string.IsNullOrEmpty(Email) && !string.IsNullOrWhiteSpace(Email)) return Email;
            else if (!string.IsNullOrEmpty(Phone) && !string.IsNullOrWhiteSpace(Phone)) return Phone;
            else return string.Empty;
        }
    }