AND 运算不能应用于可为 null 的布尔值

发布于 2024-10-06 02:33:08 字数 581 浏览 3 评论 0原文

我正在两个可为空的布尔值 (bool?) 之间应用 AND 运算 (&&),但它给了我错误

运算符 && 不能应用于 bool?bool? 类型的操作数

我如何在包含两个的语句中应用 and 运算可为空的布尔值?

另外,如果我有一个对话框结果,例如

dialog.ShowDialog () == DialogResult.OK

如何将其转换为可为空的布尔值,因为我需要放置“&&”在 if 条件中使用 this 的运算符,其另一个操作数返回可为 null 的布尔值? 代码如下:

if (dialog.ShowDialog () == DialogResult.OK && CheckProjectPath(dialog.FileName, true))

此 if 条件中的第二个操作数是可为空的布尔值。

I am applying AND operation (&&) between two nullable boolean (bool?) but it is giving me error that

Operator && cannot be applied to operands of type bool? and bool?

How do i apply and operation in my statement that contains two nullable bools?

Also if i have a dialog result like

dialog.ShowDialog () == DialogResult.OK

How can I convert it to nullable bool as i need to put '&&' operator with this in an if condition whose other operand returns a nullable bool?
Here is code:

if (dialog.ShowDialog () == DialogResult.OK && CheckProjectPath(dialog.FileName, true))

the second operand in this if condition is a nullable bool.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

摇划花蜜的午后 2024-10-13 02:33:08

如何在包含两个可为空布尔值的语句中应用和操作?

好吧,你想发生什么?这是非法的,因为如果第一个操作数为空,就会发生不好的事情。

x && 是什么意思? y 表示可空布尔值 x 和 y?那么,可为 null 的布尔值首先意味着什么?可为 null 的布尔值意味着以下三种情况之一:

  • 条件肯定为真
  • 条件肯定为假
  • 条件是真还是假,但我们不知道是哪一个

那么 x && 是什么意思? y 是什么意思?它的意思是“仅当条件 x 为真时才计算 y”,但如果 x 可为空,那么我们可能不知道 x 表示的条件是否为真

例如,假设我们有:

gotMoneyInZurich = SalesForNovemberWereMoreThanAMillionBucks() &&
    TryToDepositAMillionBucksInASwissBankAccount();

如果 SalesForNovemberWereMoreThanAMillionBucks 为 false,则不要尝试存钱,并且我们知道银行里没有钱。如果属实,则尝试存钱;如果失败了,那么我们在苏黎世就没有钱了;如果成功,我们就会这么做。

现在假设并非所有销售人员都报告了 11 月份的销售数据。我们知道 11 月份的销售额是否超过 100 万美元吗?不,11 月已经过去了;11 月已经过去了。销售额要么超过一百万美元,要么不超过一百万美元,但现在我们不知道。正确答案不是“假”,正确答案不是“真”:正确答案是“我们不知道”:null。

那么如果第一个操作数返回 null 该怎么办?我们不知道销售额是否超过一百万美元,所以尝试存钱或不尝试存钱是正确的做法吗?您是否应该根据缺失的信息采取行动?

编译器没有能力为你决定这个问题。如果你不想在结果未知的情况下存钱,那么你必须这样说:(SalesForNovemberWereMoreThanAMillionBucks() ?? false ) 表示“如果结果为 null,则将其视为 false”。

同样,如果你说:

if(x && y)
    Frob();

并且x为真,y为空,那么你应该怎么做?你是说“只有当x和y都为真时才进行Frob。x为真,我们不知道y是否为真”。那么你到底该不该Frob呢? 你不知道编译器也不知道。如果你想说的是“Frob if x is true and y is true or null”,那么说:

if(x && (y ?? true))
    Frob();

或者,“frob if x is true and y为 true,但如果 y 为 null 则不然”,然后说:

if(x && (y ?? false))
    Frob();

现在,如果您不使用 && 运算符进行短路评估,则 不要使用首先是 && 运算符。使用&运算符;它总是评估双方,所以这里没有歧义。说 x & 是完全合法的。 y 如果 xy 是可为 null 的布尔值。当然,你仍然不能在 if 中使用那个东西;这需要一个布尔值,而不是一个可为空的布尔值。但你可以说:
布尔?结果=x& y;
其中 x 和 y 是可为 null 的布尔值。

How do i apply and operation in my statement that contains two nullable bools?

Well, what do you want to happen? The reason this is illegal is because bad things happen if the first operand is null.

What does x && y mean for nullable Booleans x and y? Well, what does a nullable Boolean mean in the first place? A nullable Boolean means one of three things:

  • The condition is definitely true
  • The condition is definitely false
  • The condition is true or false but we do not know which one

So what does x && y mean? It means "evaluate y only if condition x is true", but if x is nullable then we might not know whether the condition represented by x is true or not.

For example, suppose we have:

gotMoneyInZurich = SalesForNovemberWereMoreThanAMillionBucks() &&
    TryToDepositAMillionBucksInASwissBankAccount();

If SalesForNovemberWereMoreThanAMillionBucks is false, then don't try to deposit the money, and we know we have no money in the bank. If it is true, then try to deposit the money; if that fails, then we don't have money in Zurich; if it succeeds, we do.

Now suppose that not all the salespeople have reported their sales figures for November yet. Do we know whether sales for November were more than a million bucks? No. November is in the past; either the sales were, or they weren't more than a million bucks, but right now we don't know. The correct answer is not "false", the correct answer is not "true": the correct answer is "we don't know": null.

So what should this do if null is returned by the first operand? We don't know whether sales were more than a million bucks, so is the right thing to do to try to deposit the money, or to not try to deposit the money? Should you take action on the basis of missing information, or not?

The compiler has no ability to decide this question for you. If you want to not deposit the money if the result is unknown, then you have to say that:(SalesForNovemberWereMoreThanAMillionBucks() ?? false) means "if the result was null, treat it as though it were false".

Similarly, if you say:

if(x && y)
    Frob();

and x is true, and y is null, then what should you do? You are saying "Frob only if x and y are both true. x is true, and we don't know whether y is true or not". So should you Frob or not? You don't know. The compiler doesn't know either. If what you want to say is "Frob if x is true and y is either true or null" then say that:

if(x && (y ?? true))
    Frob();

Or, "frob if x is true and y is true, but not if y is null" then say that:

if(x && (y ?? false))
    Frob();

Now, if you are not using the && operator for the purposes of short-circuiting evaluation then don't use the && operator in the first place. Use the & operator; it always evaluates both sides, so there is no ambiguity here. It is perfectly legal to say x & y if x and y are nullable bools. You still can't use that thing in an if of course; that requires a bool, not a nullable bool. But you can say:
bool? result = x & y;
where x and y are nullable bools.

我要还你自由 2024-10-13 02:33:08

您可以使用类似

bool? b1 = ...;
bool? b2 = ...;    
bool b = (b1 ?? true) && (b2 ?? false);

“您必须选择自己的默认值”之类的内容。

如何将其转换为可空布尔值,因为我需要放置“&&”在 if 条件中使用 this 的运算符,其另一个操作数返回可为 null 的布尔值?

您应该采用另一种方式:您无法对可为空的操作数执行操作,因此您必须尝试从 bool? 转换为 bool。运营商??在这里非常有用:

 if (dialog.ShowDialog () == DialogResult.OK 
     && CheckProjectPath(dialog.FileName, true) ?? false)

关于 "null-coalescing Operator" ?? :

int? a, b, c;
...
int d = a ?? b ?? c ?? -1;

如果 a、b 和 c 均为 null,则 d 变为 -1。如果其中任何一个不为空,则使用第一个值。

You can use something like

bool? b1 = ...;
bool? b2 = ...;    
bool b = (b1 ?? true) && (b2 ?? false);

You have to pick your own defaults.

How can I convert it to nullable bool as i need to put '&&' operator with this in an if condition whose other operand returns a nullable bool?

You should go the other way: You can't do operations on a nullable operand so you have to try to convert from bool? to bool. The operator ?? is very useful here:

 if (dialog.ShowDialog () == DialogResult.OK 
     && CheckProjectPath(dialog.FileName, true) ?? false)

Regarding the "null-coalescing operator" ?? :

int? a, b, c;
...
int d = a ?? b ?? c ?? -1;

if a, b and c are all null then d becomes -1. If anyone of them is not null, the first value is used.

自在安然 2024-10-13 02:33:08

我只是猜你可以尝试一下

if ((dialog.ShowDialog() == DialogResult.OK )&& 
            (CheckProjectPath(dialog.FileName, true) == true ) )

I'm just guessing you can try

if ((dialog.ShowDialog() == DialogResult.OK )&& 
            (CheckProjectPath(dialog.FileName, true) == true ) )
扶醉桌前 2024-10-13 02:33:08
if (dialog.ShowDialog () == DialogResult.OK &&
    CheckProjectPath(dialog.FileName, true).GetValueOrDefault())

没有为 null 定义 && 操作。因此,您必须确保两个布尔值都不可为 null,最简单的方法是调用可为 null 布尔值的 GetValueOrDefault() 方法。

if (dialog.ShowDialog () == DialogResult.OK &&
    CheckProjectPath(dialog.FileName, true).GetValueOrDefault())

The && operation is not defined for nulls. Therefore you have to make sure that both booleans are not nullable, and the easiest way to do that is to call the GetValueOrDefault() method of the nullable boolean.

勿挽旧人 2024-10-13 02:33:08
if (dialog.ShowDialog () == DialogResult.OK) 
{
    var checkProjectPath =  CheckProjectPath(dialog.FileName, true);
    if (checkProjectPath.HasValue && checkProjectPath)) {/*Your Action*/}
}

如果 CheckProjectPath 函数的目的是验证路径,我看不出结果应该是 bool? 的任何原因

if (dialog.ShowDialog () == DialogResult.OK) 
{
    var checkProjectPath =  CheckProjectPath(dialog.FileName, true);
    if (checkProjectPath.HasValue && checkProjectPath)) {/*Your Action*/}
}

If the purpose of the CheckProjectPath function is to validate a Path I cannot see any reason why the result should be bool?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文