开发者

null coalesce operator in VB.Net(8)

开发者 https://www.devze.com 2023-02-02 15:53 出处:网络
i\'m afraid that this is a stupid question, but i must assume that i have pro开发者_Python百科grammed VB.Net too long and now can\'t figure out how to convert this C# null coalescing operator into VB.

i'm afraid that this is a stupid question, but i must assume that i have pro开发者_Python百科grammed 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.


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 .


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消