Null coalesce statement in VB.Net (8)

I'm afraid this is a stupid question, but I have to assume that I have been programming VB.Net for too long and now I can’t figure out how to convert this C # null coalescing statement to VB.Net:

if( Convert.ToBoolean(ViewState[tp.UniqueID + "_Display"] ?? true) == false ){} 

I know IIF-Function , but I'm not sure how to use it here, and if it gives the correct result (in IIF how expressions are evaluated). Please help shed light on the dark.

EDIT : if you want to see the source of this: forums.asp.net There you can see the solution that throws Option Strict On disallows implicit conversions from 'Object' to 'Boolean' compiler exception.

+3
c # conditional-operator visual-studio-2005
Jan 04 2018-11-11T00:
source share
8 answers

For 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 is actually equivalent to the C # version

 Not CBool(IIf(IsNothing(ViewState(tp.UniqueID + "_Display")), True, ViewState(tp.UniqueID + "_Display"))) 
+1
Jan 04 2018-11-14T00:
source share
β€” -

You want an if statement (Not an IIF function). It can be used as the equivalent of both the conditional operator ?: And the operator ?? zero coalescing from C #, depending on whether it 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 

This at least still gives a short circuit.

+5
Jan 04 2018-11-11T00:
source share

If you use vb 9, you can "if" the ternary operator .

+2
Jan 04 2018-11-11T00:
source share

This should work:

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

I did not use the IIf operator to simplify :)

+1
Jan 04 2018-11-14T00:
source share

Use IIF for VB .

IIf Feature Reference

 IIF( IIF(Convert.ToBoolean(ViewState[tp.UniqueID + "_Display"] = Nothing, True, ViewState[tp.UniqueID + "_Display"]), Success(), Failure()) 
0
Jan 04 2018-11-14T00:
source share

Perhaps you are trying to make it too difficult. Try the following:

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

Remember that ViewState returns an object box, nothing prevents you from comparing True and False directly with each other. = True is optional if you have Option Strict Off .

As an alternative

 If Object.Equals(ViewState[tp.UniqueID + "_Display"], True) Then 
0
Jan 04 2018-11-11T00:
source share

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 
0
Jan 04 2018-11-11T00:
source share

The given example is bad - so bad it is almost shameful. In a literal sense, this is a call that evaluates only two different contexts: whether the area with square brackets is executed or skipped.

Here is a logical analysis to better explain what:

  • ViewState [tp.UniqueID + "_Display"] will evaluate:

    • falsely
    • right,
    • null or
    • something else

If the published source, if the estimate is false, the zero-coalescing operation is shorted and forces a true estimate of "== false". Then curly-bracket-content is executed.

If this rating is something else, then null-coalesces evaluating to "true" and forces a false rating of "== false". Then the contents of the italic bracket are skipped.

So, in fact, the correct and very simple way to write the original source:

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

In particular, it does not have a zero join operation.

The problem with this is that the example is not enough to even justify the use of the zero-coalescing operation, and this predicts the need to ever β€œconvert” the operation to Visual Basic.

0
Apr 23 '15 at 16:39
source share



All Articles