如何显示是/否,而不是True / False for bool?在c# [英] How to display Yes/No instead of True/False for bool? in c#
问题描述
我收到错误无法将类型'string'隐式转换为'bool'如何返回是或否而不是true / false?
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; }
}
推荐答案
如果返回类型为<在这种情况下,code> bool (或 bool?
)返回一个 bool
:
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...
如何显示是/否...
此代码不显示任何东西,这是一个属性一个对象,而不是UI组件。在UI中,您可以移动ay你喜欢使用这个属性作为一个标志:
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"; }
}
这篇关于如何显示是/否,而不是True / False for bool?在c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!