Sreejith Sree
Sreejith Sree

Reputation: 3681

xamarin forms: How to bind another value if the current binding value is empty or null?

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?

Upvotes: 1

Views: 746

Answers (1)

FreakyAli
FreakyAli

Reputation: 16489

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;
        }
    }

Upvotes: 1

Related Questions