Hello friends, I have a question, how can I put a condition within a property? I want to do this based on another property of the same model, here is my example
public string Last4 { get; set; }
public string MY { get; set; }
public bool IsSelected { get; set; }
public MopType CardIcon { get; set; }
public string SelectedString {
get;
// aqui quiero algo como
if(IsSelected){ set = "Selecionado"}
else{ set = " " }
}
I came up with this, but my _selectedString always returns null
private string _selectedString;
public string SelectedString {
get { return _selectedString; }
set {
if (IsSelected)
{
_selectedString = "Selecionado";
}
else
{
//DEFAULT Value.
_selectedString = " ";
}
}
}
You want that when you change the IsSelected property, then the code of the SelectedString property is changed. Well, then what you have to do is control that property and change the text in the other one depending on that property.
Quite simply, your property where we have to work is IsSelected.
Another very simple option is to simply return the value of
SelectedString
based on the ofIsSelected
:and because you do not use the ternary operators, raising your case, it would be more or less like this
It is not a good idea to keep changing the value of a property depending on another value. By convention, properties are for returning and setting values. Your problem is better managed from a method.
I have put you SelectedString in the form of a method. So you only have to worry about handling the IsSelected bool, depending on how you want to handle it in your application.