收到错误消息 - 无法类型“string”隐式转换为“布尔” [英] Getting Error Msg - Cannot implicitly convert type 'string' to 'bool'
本文介绍了收到错误消息 - 无法类型“string”隐式转换为“布尔”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我检查值一个文本框来触发条件语句,但我收到错误消息。
I am checking for values in a textbox to trigger a conditional statement, but am getting error messages.
if (txtAge.Text = "49") || (txtAge.Text = "59")
{
txtNote.Text = "A valid picture ID must be submitted";
}
我得到该错误消息的无法隐式转换类型'字符串到布尔的
我怎样才能解决这个问题?
How can I resolve this?
推荐答案
当你输入:
if (txtAge.Text = "49")
这基本上都是指派49给 txtAge.Text
,然后。返回的值作为字符串(等于49)
This basically is assigning "49" to txtAge.Text
, and then returning that value as a string (equal to "49").
这是一样的,从本质上讲,是这样做的:
This is the same, essentially, as doing:
string tmp = txtAge.Text = "49";
if (tmp) { //...
但是,你不能这样做,如果(stringExpression),因为if语句只适用于一个布尔值。你最有可能想输入:
However, you cannot do "if (stringExpression)", since an if statement only works on a boolean. You most likely wanted to type:
if (txtAge.Text == "49" || txtAge.Text == "59")
{
这篇关于收到错误消息 - 无法类型“string”隐式转换为“布尔”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文