为什么我得到“无法隐式转换类型'十进制'到'bool'”错误?

时间:2021-12-29 16:31:13

I want to hide some things when the value of the numericUpDown is changed so I wrote this:

我想在numericUpDown的值改变时隐藏一些东西,所以我写了这个:

if (numericUpDown1.Value = 1)
{
    Label1.Hide();
}

but I get this error message:

但我收到此错误消息:

Cannot implicitly convert type 'decimal' to 'bool'

无法将类型'decimal'隐式转换为'bool'

Why is this happening?

为什么会这样?

3 个解决方案

#1


I think you mean "if (numericUpDown1.Value == 1)".

我认为你的意思是“if(numericUpDown1.Value == 1)”。

In most languages, "==" is the test for equality, while "=" is the assignment operator.

在大多数语言中,“==”是对相等性的测试,而“=”是赋值运算符。

#2


What language are you using with the single "=" sign? In VB you would be comparing and in C# you would be assigning a number with an "if" statement. A way to protect yourself from this would be to list the number first: if (1 = numericUpDown) which would be fine if comparing was allowed in that language and bad you had intended to make an assignment. The mistake would jump out at you!

您使用单个“=”符号使用什么语言?在VB中,您将进行比较,在C#中,您将使用“if”语句分配一个数字。保护自己的一种方法是首先列出数字:if(1 = numericUpDown),如果在该语言中允许进行比较,那么就没有问题。这个错误会跳出来!

#3


You're not performing a comparison (change = to ==) ... Try:

你没有进行比较(更改= ==)...尝试:

if (numericUpDown1.Value == 1)
{
    Label1.Hide();
}

#1


I think you mean "if (numericUpDown1.Value == 1)".

我认为你的意思是“if(numericUpDown1.Value == 1)”。

In most languages, "==" is the test for equality, while "=" is the assignment operator.

在大多数语言中,“==”是对相等性的测试,而“=”是赋值运算符。

#2


What language are you using with the single "=" sign? In VB you would be comparing and in C# you would be assigning a number with an "if" statement. A way to protect yourself from this would be to list the number first: if (1 = numericUpDown) which would be fine if comparing was allowed in that language and bad you had intended to make an assignment. The mistake would jump out at you!

您使用单个“=”符号使用什么语言?在VB中,您将进行比较,在C#中,您将使用“if”语句分配一个数字。保护自己的一种方法是首先列出数字:if(1 = numericUpDown),如果在该语言中允许进行比较,那么就没有问题。这个错误会跳出来!

#3


You're not performing a comparison (change = to ==) ... Try:

你没有进行比较(更改= ==)...尝试:

if (numericUpDown1.Value == 1)
{
    Label1.Hide();
}