nullreferenceexception - vb.net: how is iif giving a null reference exception? -


Seeing the many posts / articles about using IIF, it all started when I used CIC / C # Tried to use IIF as a conditional operator in the choice of C ++.

Borrowed from:

After that I have learned about if () method in vb.net.

So in the end I wrote a function that gives the appropriate value (which uses reflection) to achieve the objective. But that method (which I wrote) did not throw any exception. Actually I was able to check the type of futility within the function. return value (as some form of form, byVal Property Name as string) in the form of a object, if MyType is nothing then return the string. PropertyInfo () = MyType.GetType (). GetProperties () Return arrPropInfo.Where (function (x) x.Name = PropertyName) .emem (0) .GetValue (MyType, nothing) End function

My question is, what Is there something written inside the IIF to throw such an error? - A NullReferenceException

  If MyType is nothing then throw the new NullReferenceException ()   

Or is there something bigger at work here? So suppose that if I want to write a function like iif, which has some null in the logic list, is it the only way to do it in the above manner?

The distinction is that ?: and if () are operators IIf () is not the operator; It is a plain old function that you can apply yourself to:

  Public Shared Function IIf (In the form of Boolean, as an object in the form of an expression, by- Fall part) If returning as an object then return to TruePart or FalsePart End Function   

This means that all the arguments passed in the function should be evaluated before < / Em> function, such as a Many other functions. There is nothing in the IIF that throws an exception - this is a normal process for evaluating the passages passed for the function.

You also have to pay attention that it is absolutely fine to pass zero / nothing to a function. You had seen an exception in your code, because the expression you were passing was a null reference, which can not be evaluated - say, if you do something then try to use it. The length property of a redundant string variable.

On the other hand, if () operator does not have this problem because it is only this: Operator In addition to this, it is an operator who takes advantage of short-circuit evaluation, such as only returning expression Should be evaluated. If () is also a good way to be safe-protected, although you can repeat typed security in the normal IIF (T) () function, if you wish.

Comments