VB.Net 中的空合并运算符(8)
恐怕这是一个愚蠢的问题,但我必须假设我已经编写 VB.Net 太长时间了,现在不知道如何转换这个 C# null 合并运算符 到 VB.Net 中:
if( Convert.ToBoolean(ViewState[tp.UniqueID + "_Display"] ?? true) == false ){}
我知道 IIF-Function 但我不确定如何在这里使用它以及它是否给出正确的结果(在 IIF 中,两个表达式都被计算)。请帮助照亮黑暗。
编辑:如果您想查看其来源:论坛.asp.net 您可以在此处看到一个解决方案,该解决方案生成 Option Strict On 不允许从“Object”到“Boolean”的隐式转换
编译器异常。
i'm afraid that this is a stupid question, but i must assume that i have programmed VB.Net too long and now can't figure out how to convert this C# null coalescing operator into VB.Net:
if( Convert.ToBoolean(ViewState[tp.UniqueID + "_Display"] ?? true) == false ){}
I know the IIF-Function but i'm not sure how to use it here and if it gives the correct result(in IIF both expressions are being evaluated). Please help to shed light on the dark.
EDIT: if you want to see the source of this: forums.asp.net
There you can see a solution that generates a Option Strict On disallows implicit conversions from 'Object' to 'Boolean'
compiler exception.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您需要 If 运算符(不是IIF 函数)。它可以用作 C# 中
?:
条件运算符和??
空合并运算符的等效项,具体取决于它是传递 3 个参数还是 2 个参数像这样的东西:
这至少仍然会让你短路。
You want the If operator (Not the IIF function). It can be used as the equivalent of both the
?:
conditional operator and the??
null coalescing operator from C#, depending on whether it's passed 3 arguments or 2You really want something like:
Which at least still gives you short-circuiting.
如果您使用的是 vb 9,则可以 "if" 三元运算符 。
if you are using vb 9 you can you "if" ternary operator .
这应该可行:
我没有使用 IIf 运算符来简化:)
This should work:
I didn't use the IIf operator to simplify :)
已经有一段时间了,但我认为这就是你想要的:
编辑,作者:Tim(OP):
这实际上等于 C# 版本
Been a while but I think this is what you want:
EDIT by Tim(OP):
This is what actually equals the C# version
将
IIF
用于VB
。IIf 函数参考
Use
IIF
forVB
.IIf Function Reference
也许你想让这件事变得太难。试试这个:
记住,ViewState 返回一个装箱的
对象
,没有什么可以阻止您直接相互比较True
和False
。当您设置Option Strict Off
时,= True
是可选的。或者
Maybe you're trying to make this too hard. Try this:
Remember, the ViewState returns a boxed
object
, nothing stops you from comparingTrue
andFalse
directly with one another. The= True
is optional when you haveOption Strict Off
.Alternatively
将
String
函数IsNullOrEmpty
与request
对象结合使用。Use the
String
functionIsNullOrEmpty
with therequest
object.提供的例子很糟糕——糟糕到几乎可耻。它实际上有一个调用,该调用仅评估两个不同的上下文,以确定括号内的区域是执行还是被跳过。
以下是逻辑分析,可以更好地解释:
ViewState[tp.UniqueID + "_Display"] 将计算为:
如果评估为 false,则使用已发布的源,空合并操作会短路并强制为 true评估为“== false”。然后执行大括号内容。
如果该评估是其他任何值,则评估空值合并为“true”并强制在“== false”处进行错误评估。然后跳过大括号内容。
因此,实际上编写原始源代码的正确且非常简单的方法是:
值得注意的是,这没有空合并操作。
其中的问题是,该示例甚至不足以证明使用空合并操作的合理性,并且表明需要将该操作“转换”为 Visual Basic。
The exmample provided is bad -- so bad its virtually shameful. It literally has a call that only evaluates to two different contexts to whether a bracketed region executes or gets skipped over.
Here is logical analysis to better explain that:
ViewState[tp.UniqueID + "_Display"] will evaluate to:
With the posted source if the evaluation is false, the null-coalesce operation is short-circuited and forces a true evaluation at "== false". Then curly-bracket-content executes.
If that evaluation is anything else then the evaluation null-coalesces to 'true' and forces a false evaluation at "== false". Then curly-bracket-content is skipped over.
So actually the proper and very simple way to write the original source is:
Notably this has no null-coalesce opertation.
The problem therein becomes that the example is inadequate to even justify use of a null-coalesce operation and that predicates the need to ever 'convert' the operation to Visual Basic.