null coalesce operator in VB.Net(8)

◇◆丶佛笑我妖孽 提交于 2019-12-10 10:05:32

问题


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.


回答1:


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



回答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 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.




回答3:


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




回答4:


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




回答5:


Use IIF for VB.

IIf Function Reference

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



回答6:


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



回答7:


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



回答8:


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.



来源:https://stackoverflow.com/questions/4594446/null-coalesce-operator-in-vb-net8

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