2

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.

Patrick McDonald
  • 64,141
  • 14
  • 108
  • 120
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 1
    That is not the ternary operator you are using, it is the null coalescing operator. Which means that if the part left to the operator is not null use that value, otherwise use the value to the right. – Tomas Jansson Jan 04 '11 at 14:11
  • 2
    Might I recommend putting `!`(C#) or `Not`(VB) at the beginning of the if condition and removing the `== false`. I hate to see boolean comparisons. – juharr Jan 04 '11 at 14:11
  • I appreciate your comment, but sometimes boolean comparison adds readability :) – usr-local-ΕΨΗΕΛΩΝ Jan 04 '11 at 14:17
  • @Tomas Jansson: Thanks for clearify that this is not the ternary operator. That was the main cause for my confusion. – Tim Schmelter Jan 04 '11 at 14:34
  • 1
    possible duplicate of [Is there a VB.NET equivalent for C#'s ?? operator?](http://stackoverflow.com/questions/403445/is-there-a-vb-net-equivalent-for-cs-operator) – Ryan Gates Jul 17 '13 at 15:29

8 Answers8

5

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.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
2

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

Biswanath
  • 9,075
  • 12
  • 44
  • 58
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")))
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Mike
  • 5,437
  • 7
  • 45
  • 62
  • Nope, he wants to convert the value in ViewState if it is not null. I suspect it is a string that is either "True" or "False". – juharr Jan 04 '11 at 14:13
  • @juharr: the value can be nothing or the booleans true/false. @Mike: i normally avoid the IsNothing-function but i think this is the best/compactest solution in this case(VB8). – Tim Schmelter Jan 04 '11 at 15:28
1

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

usr-local-ΕΨΗΕΛΩΝ
  • 26,101
  • 30
  • 154
  • 305
  • 1
    IIf is the VB.NET alternative, but it certainly wasn't a patch made because C# had something that VB.NET didn't. Classic VB (which was around long before C#) had the IIf statement as well (along with VBA). – Justin Niessner Jan 04 '11 at 14:11
  • 1
    VB has had the If *operator* for quite a while, and it's the equivalent of both the `?:` conditional operator and the `??` null coalescing operator. – Damien_The_Unbeliever Jan 04 '11 at 14:16
  • This is a mix of C# and VB.Net. If i change it to `Convert.ToBoolean(IIf(ViewState(tp.UniqueID + "_Display") Is Nothing, True, ViewState(tp.UniqueID + "_Display")) = False)`, i will get an exception: `Option Strict On disallows implicit conversions from 'Object' to 'Boolean'` – Tim Schmelter Jan 04 '11 at 14:41
0

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.

Hardryv
  • 755
  • 7
  • 12
0

Use IIF for VB.

IIf Function Reference

IIF(
    IIF(Convert.ToBoolean(ViewState[tp.UniqueID + "_Display"] = Nothing, 
          True, 
          ViewState[tp.UniqueID + "_Display"]), 
    Success(), 
    Failure())
decyclone
  • 30,394
  • 6
  • 63
  • 80
0

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
Juliet
  • 80,494
  • 45
  • 196
  • 228
  • I'm ever have Option Strict On. So i get an `Option Strict On disallows implicit conversions from 'Object' to 'Boolean'` – Tim Schmelter Jan 04 '11 at 14:42
0

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