0

The following works:

If 1=1
    rdoYes.checked = True
Else
    rdoNo.checked = True
End If

However, the following doesn't work:

IIF(1=1, rdoYes.checked = True, rdoNo.checked = True)

Why is this?

Thanks!

Curtis
  • 101,612
  • 66
  • 270
  • 352
  • possible duplicate of [Is there a conditional ternary operator in VB.NET?](http://stackoverflow.com/questions/576431/is-there-a-conditional-ternary-operator-in-vb-net) – MarkJ Feb 21 '12 at 08:53
  • Apologies I meant to vote to close as a duplicate of this http://stackoverflow.com/questions/428959/using-vb-net-iif-i-get-nullreferenceexception – MarkJ Feb 21 '12 at 08:54
  • For anyone coming from google a solution to this problem: http://www.adamjamesnaylor.com/2012/10/29/SQL-Server-Reporting-Services-IIf-Function-Evaluating-True-And-False-Parameters.aspx – Adam Naylor Oct 29 '12 at 13:19

4 Answers4

7

It does "work"; it just doesn't do what you want.

IIf in VB.NET is a function (don't use it, ever, by the way), which takes these parameters:

  1. A Boolean condition to check
  2. An Object to return if the condition is True
  3. A different Object to return if the condition is False

In your usage, your condition is 1 = 1; then your two other parameters are rdoYes.Checked = True and rdoNo.Checked = True, both Boolean expressions from the VB compiler's perspective (so, really, they're equivalent to the simpler rdoYes.Checked and rdoNo.Checked).

Remember that in VB.NET, the = sign is only an assignment if it is on its own line. This is how the compiler distinguishes between statements such as x = 5 and If x = 5 Then.

This is not directly related to your question, but you should also be aware that IIf is deprecated and you should almost always favor If instead:

' Let us just suppose it made sense to write this: '
' Notice the If instead of IIf. '
Dim result = If(1 = 1, rdoYes.Checked, rdoNo.Checked)
Community
  • 1
  • 1
Dan Tao
  • 125,917
  • 54
  • 300
  • 447
3

The IIF() function will return something based on what you enter for the first parameter. Since VB.Net doesn't differ between = as in assignment and = as in comparison (== in many other languages), the second statement is ambiguous.


You can do this with using late binding (delegates in VB.Net):

(Function(c) InlineAssignHelper(c.Checked, true)).Invoke(IIf(1 = 1, chkYes, chkNo))

  Private Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
    target = value
    Return value
  End Function
Jan Jongboom
  • 26,598
  • 9
  • 83
  • 120
Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402
  • I added an example how this cán be achieved in VB.Net – Jan Jongboom Sep 13 '10 at 15:33
  • 1
    I believe it's actually *not* ambiguous; it **is** the comparison (the `=` symbol is only ever treated as assignment when it is by itself as an expression). Notice that the OP's code does *compile*; it just doesn't *do* what he wants. – Dan Tao Sep 13 '10 at 15:33
1

Because IIf takes expressions and returns a result of one of them, and rdoYes.checked = True is not an expression and cannot be returned.

wRAR
  • 25,009
  • 4
  • 84
  • 97
1

iif doesn't do what you think it does -- the important part is the return from it, so you might be able to do:

iif(1=1, rdoYes, rdoNo).checked = True

(I'm not sure that's valid VB ... it's been more than a decade since I've had to code in it)

Joe
  • 2,547
  • 1
  • 18
  • 27
  • valid Vb would probably be more like ctype(iif(1=1, rdoYes, rdoNo), radiobutton).checked = true. Thanks :) – Curtis Sep 13 '10 at 15:29