VB.Net 中的空合并运算符(8)

发布于 2024-10-10 10:16:23 字数 674 浏览 10 评论 0原文

恐怕这是一个愚蠢的问题,但我必须假设我已经编写 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 技术交流群。

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

发布评论

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

评论(8

夕嗳→ 2024-10-17 10:16:23

您需要 If 运算符(不是IIF 函数)。它可以用作 C# 中 ?: 条件运算符和 ?? 空合并运算符的等效项,具体取决于它是传递 3 个参数还是 2 个


参数像这样的东西:

If Not ViewState[tp.UniqueID + "_Display"] is Nothing AndAlso Not CType(ViewState[tp.UniqueID + "_Display"],Boolean) Then

End If

这至少仍然会让你短路。

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 2


You really want something like:

If Not ViewState[tp.UniqueID + "_Display"] is Nothing AndAlso Not CType(ViewState[tp.UniqueID + "_Display"],Boolean) Then

End If

Which at least still gives you short-circuiting.

世界和平 2024-10-17 10:16:23

如果您使用的是 vb 9,则可以 "if" 三元运算符

if you are using vb 9 you can you "if" ternary operator .

污味仙女 2024-10-17 10:16:23

这应该可行:

If (ViewState(tp.UniqueID + "_Display") IsNot Nothing OrElse Convert.ToBoolean(ViewState(tp.UniqueID + "_Display") = false) Then ... End If

我没有使用 IIf 运算符来简化:)

This should work:

If (ViewState(tp.UniqueID + "_Display") IsNot Nothing OrElse Convert.ToBoolean(ViewState(tp.UniqueID + "_Display") = false) Then ... End If

I didn't use the IIf operator to simplify :)

三人与歌 2024-10-17 10:16:23

已经有一段时间了,但我认为这就是你想要的:

CBool(IIf(IsNothing(ViewState(tp.UniqueID + "_Display")), True, False))

编辑,作者:Tim(OP):

这实际上等于 C# 版本

Not CBool(IIf(IsNothing(ViewState(tp.UniqueID + "_Display")), True, ViewState(tp.UniqueID + "_Display")))

Been a while but I think this is what you want:

CBool(IIf(IsNothing(ViewState(tp.UniqueID + "_Display")), True, False))

EDIT by Tim(OP):

This is what actually equals the C# version

Not CBool(IIf(IsNothing(ViewState(tp.UniqueID + "_Display")), True, ViewState(tp.UniqueID + "_Display")))
悲歌长辞 2024-10-17 10:16:23

IIF 用于VB

IIf 函数参考

IIF(
    IIF(Convert.ToBoolean(ViewState[tp.UniqueID + "_Display"] = Nothing, 
          True, 
          ViewState[tp.UniqueID + "_Display"]), 
    Success(), 
    Failure())

Use IIF for VB.

IIf Function Reference

IIF(
    IIF(Convert.ToBoolean(ViewState[tp.UniqueID + "_Display"] = Nothing, 
          True, 
          ViewState[tp.UniqueID + "_Display"]), 
    Success(), 
    Failure())
苏佲洛 2024-10-17 10:16:23

也许你想让这件事变得太难。试试这个:

If ViewState[tp.UniqueID + "_Display"] = True Then ...

记住,ViewState 返回一个装箱的对象,没有什么可以阻止您直接相互比较TrueFalse。当您设置Option Strict Off 时,= True 是可选的。

或者

If Object.Equals(ViewState[tp.UniqueID + "_Display"], True) Then

Maybe you're trying to make this too hard. Try this:

If ViewState[tp.UniqueID + "_Display"] = True Then ...

Remember, the ViewState returns a boxed object, nothing stops you from comparing True and False directly with one another. The = True is optional when you have Option Strict Off.

Alternatively

If Object.Equals(ViewState[tp.UniqueID + "_Display"], True) Then
兔姬 2024-10-17 10:16:23

String 函数 IsNullOrEmptyrequest 对象结合使用。

Dim display As Boolean = False
If String.IsNullOrEmpty(Request.QueryString("UID")) Then
  display = Convert.ToBoolean(Request.QueryString("UID"))
End If

Use the String function IsNullOrEmpty with the request object.

Dim display As Boolean = False
If String.IsNullOrEmpty(Request.QueryString("UID")) Then
  display = Convert.ToBoolean(Request.QueryString("UID"))
End If
游魂 2024-10-17 10:16:23

提供的例子很糟糕——糟糕到几乎可耻。它实际上有一个调用,该调用仅评估两个不同的上下文,以确定括号内的区域是执行还是被跳过。

以下是逻辑分析,可以更好地解释:

  • ViewState[tp.UniqueID + "_Display"] 将计算为:

    • 错误,
    • 正确,
    • 空,或
    • 其他东西

如果评估为 false,则使用已发布的源,空合并操作会短路并强制为 true评估为“== false”。然后执行大括号内容。

如果该评估是其他任何值,则评估空值合并为“true”并强制在“== false”处进行错误评估。然后跳过大括号内容。

因此,实际上编写原始源代码的正确且非常简单的方法是:

if( Convert.ToBoolean( ViewState[tp.UniqueID + "_Display"] ) == false) {
    // do something
}

值得注意的是,这没有空合并操作。

其中的问题是,该示例甚至不足以证明使用空合并操作的合理性,并且表明需要将该操作“转换”为 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:

    • false,
    • true,
    • null, or
    • something else

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:

if( Convert.ToBoolean( ViewState[tp.UniqueID + "_Display"] ) == false) {
    // do something
}

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.

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