Just for the record, here is the difference between If and IIf:
IIf(condition, true-part, false-part):
- This is the old VB6/VBA Function
- The function always returns an Object type, so if you want to use the methods or properties of the chosen object, you have to re-cast it with DirectCast or CType or the Convert.* Functions to its original type
- Because of this, if true-part and false-part are of different types there is no matter, the result is just an object anyway
If(condition, true-part, false-part):
- This is the new VB.NET Function
- The result type is the type of the chosen part, true-part or false-part
- This doesn't work, if Strict Mode is switched on and the two parts are of different types. In Strict Mode they have to be of the same type, otherwise you will get an Exception
- If you really need to have two parts of different types, switch off Strict Mode (or use IIf)
- I didn't try so far if Strict Mode allows objects of different type but inherited from the same base or implementing the same Interface. The Microsoft documentation isn't quite helpful about this issue. Maybe somebody here knows it.