null coalesce operator in VB.Net(8)

一笑奈何 提交于 2019-12-05 19:39:16
Mike

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")))

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.

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

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 :)

Use IIF for VB.

IIf Function Reference

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

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!