I saw this in some vb.net source code:
Dim sTest As String = "" & drTest("column")
I was told that if drTest(“column”) is nothing, then sTest will be assigned “”, so it is in effect doing:
Dim sTest As String = If("",Nothing,drTest("column"))
What is the downside of doing it the first way I showed?
What is the difference between using If and IIf?
Ifis the ternary conditional operator.IIfis an ordinary function that is implemented approximately as follows:This means that no matter the value of
condition, both its other arguments are always evaluated. This doesn’t happen with theIfoperator.But your code is actually doing something completely different:
This makes no sense and shouldn’t even compile (or at least give a warning). Your first code is fine – it works always and there is no downside. It’s not clear what the second code is even trying to achieve.