Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

First operand in a binary 'If' expression must be nullable or a reference type

An If expression can take either two or three arguments. When you send only two arguments, the first argument must be a reference type or a nullable type. If the first argument evaluates to anything other than Nothing, its value is returned. If the first argument evaluates to Nothing, the second argument is evaluated and returned.

For example, the following code contains two If expressions, one with three arguments and one with two arguments. The expressions calculate and return the same value.

' firstChoice is a nullable value type.
Dim firstChoice? As Integer = Nothing
Dim secondChoice As Integer = 1128
' If expression with three arguments.
Console.WriteLine(If(firstChoice IsNot Nothing, firstChoice, secondChoice))
' If expression with two arguments.
Console.WriteLine(If(firstChoice, secondChoice))

The following expressions cause this error:

Dim choice1 = 4
Dim choice2 = 5
Dim booleanVar = True

' Not valid.
'Console.WriteLine(If(choice1 < choice2, 1))
' Not valid.
'Console.WriteLine(If(booleanVar, "Test returns True."))

Error ID: BC33107

To correct this error

  • If you cannot change the code so that the first argument is a nullable type or reference type, consider converting to a three-argument If expression, or to an If...Then...Else statement.

    Console.WriteLine(If(choice1 < choice2, 1, 2))
    Console.WriteLine(If(booleanVar, "Test returns True.", "Test returns False."))
    
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft