Reputation: 8592
I get the error "Cannot implicitly convert type 'string' to 'bool'. How do I return 'Yes' or 'No' instead of true/false?
public bool? BuyerSampleSent
{
get { bool result;
Boolean.TryParse(this.repository.BuyerSampleSent.ToString(), out result);
return result ? "Yes" : "No";
}
set { this.repository.BuyerSampleSent = value; }
}
Upvotes: 0
Views: 7185
Reputation: 943
You cannot return a bool value "Yes" or "No". In C#, bool is a keyword for Boolean Data Type.You cannot override this behavior of a keyword.
Read more about C# Boolean Data Type here.
In your case you can do the following:
public string BuyerSampleSent
{
get
{
string result= "No";
if (this.repository.BuyerSampleSent.Equals("true",StringComparisson.OrdinalIgnoreCase)) // <-- Performance here
result = "Yes";
return result;
}
set
{
this.repository.BuyerSampleSent = value;
}
}
Upvotes: 0
Reputation: 218960
You can't return a string if the return type is bool
(or bool?
in this case). You return a bool
:
return result;
Notice, however, that you ask...
How to display Yes/No...
This code isn't displaying anything. This is a property on an object, not a UI component. In the UI you can display whatever you like using this property as a flag:
someObject.BuyerSampleSent ? "Yes" : "No"
Conversely, if you want a display-friendly message on the object itself (perhaps it's a view model?) then you can add a property for that message:
public string BuyerSampleSentMessage
{
get { return this.BuyerSampleSent ? "Yes" : "No"; }
}
Upvotes: 7
Reputation: 693
Your method returns a bool as @Pierre-Luc points out. You need to change that to a String.
Upvotes: 0