I am doing a little debugging, and so I want to log the eventArgs value
I have a simple line that basically does:
logLine = "e.Value: " + IIf(e.Value Is Nothing, "", e.Value.ToString())
The way I understand the IIF function, if the e.Value is Nothing (null) then it should return the empty string, if not it should return the .ToString of the value. I am, however getting a NullReferenceException. This doesn't make sense to me.
Any idea's?
VB does not do short-circuiting evaluation in Iif. In your case, e.Value.ToString() is being evaluated no matter whether e.Value is nothing.
This is the expected behaviour.
IIF is a function; therefore the parameters for the function will be evaluated before sending it to the function.
In contrast, the ternary operator in C# is a language construct that prevents the evaluation of the second parameter if the expression of the ternary is true.
来源:https://stackoverflow.com/questions/428959/using-vb-net-iif-i-get-nullreferenceexception