与操作能不能为空的bool之间施加 [英] AND operation cannot be applied between nullable bools

查看:148
本文介绍了与操作能不能为空的bool之间施加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2可空布尔(?布尔),但它的

我申请经营( &放大器;&)是给我的错误




运营商&放大器;&安培; 不能被应用到操作数类型布尔?布尔?




我该如何申请和操作我的声明,其中包含两个可为空的bool?



另外,如果我有一个对话的结果像

  dialog.ShowDialog()== DialogResult.OK 

我怎样才能将其转换为可空布尔,因为我需要把'和;&放大器;'这个运营商如果条件的其他操作数返回一个可空布尔?
下面是代码:

 如果(dialog.ShowDialog()== DialogResult.OK和放大器;&安培; CheckProjectPath( dialog.FileName,真))

在此第二个操作数,如果条件是一个可空布尔。


解决方案

我该如何申请,并在我的发言操作,其中包含两个可为空的bool?




好了,你想要什么发生?究其原因,这是非法的,因为不好的事情发生,如果第一个操作数为null



这是什么 X&功放;&安培;是意味着可空布尔x和y?那么,什么是一个可空布尔摆在首位的意思吗?可空布尔意味着三件事之一:




  • 的条件肯定是真的

  • 的条件是肯定是假的

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



所以,哪些呢 X&功放;&安培;是是什么意思?它的意思是计算y只有当条件x是真的,但如果x为空,然后的我们可能不知道X代表的条件是否是真还是假



例如,假设我们有:

  gotMoneyInZurich = SalesForNovemberWereMoreThanAMillionBucks()及和放大器; 
TryToDepositAMillionBucksInASwissBankAccount();

如果SalesForNovemberWereMoreThanAMillionBucks是假的,那么不要尝试存入的钱,我们知道我们有没有钱存进银行。如果这是真的话,尽量把钱存;如果失败,那么我们没有在苏黎世的钱;如果成功,我们做的。



现在假设并不是所有的销售人员都报告他们的销售数字尚未月。我们知道销售月是否超过一百万美元?十一月号是过去;无论是销售,还是他们不超过一百万美元,但是的现在我们不知道的。正确的答案是不是假,正确答案是不是真:正确的答案是:我们不知道:空



因此​​,什么应该该怎么做,如果null被第一个操作数返回?我们不知道是否销售额比一百万美元以上,所以是正确的事情,试图存钱,还是不要尝试存钱?如果你采取行动缺少的信息,或没有的基础上?



编译器有没有能力来决定这个问题要问你。如果您想不存的钱,如果结果是未知的,那么你不得不说:(SalesForNovemberWereMoreThanAMillionBucks()??假)表示如果结果为null,把它

 <$ C $:就好像是假的



同样的,如果你说C>如果(X放大器;&安培; Y)
FROB();

和x为true,y是零,那么你该怎么办?您是说FROB仅当x和y都为true。x是真实的,我们不知道y的值是真还是假。所以,如果你FROB与否?的你不知道的。 编译器不知道任何如果你想说什么是FROB如果x是真实的,y是真或空则说:

 如果(X放大器;&安培;(Y ??真))
FROB();

或者,如果FROB x为true,y是真实的,但如果y不为空,那么说:

 如果(X放大器;&安培;(Y ?? FALSE))
FROB();

现在,如果你没有使用&放大器;&安培;运营商而言短路评价和不使用放大器&;&安培;运营商首先即可。使用&放大器;运营商;它总是计算两侧,所以这里没有歧义。这是完全合法的说X&安培; Y如果x和y是可为空的bool;结果将是零,如果x或y为null,如果X和Y均为真,否则为假。您仍然不能使用的东西在一个如果,当然,这需要一个布尔值,而不是为空的布尔。但是,你可以说:

 布尔?结果= X&放大器; ÿ; 



其中x和y可以为空的bool。


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.

解决方案

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; the result will be null if x or y are null, true if x and y are both true, and false otherwise. 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.

这篇关于与操作能不能为空的bool之间施加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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