获取错误消息 - 无法将类型“字符串"隐式转换为“布尔" [英] Getting Error Msg - Cannot implicitly convert type 'string' to 'bool'

查看:27
本文介绍了获取错误消息 - 无法将类型“字符串"隐式转换为“布尔"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检查文本框中的值以触发条件语句,但收到错误消息.

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").

这本质上是一样的:

string tmp = txtAge.Text = "49";
if (tmp) { //...

但是,您不能执行if (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")
{

这篇关于获取错误消息 - 无法将类型“字符串"隐式转换为“布尔"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆