开发者

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

Been looking at a lot of post/articles about using Iif(). All of this started when I tried to use IIf() like a conditional operator much in the likes of C/C#/C++ etc.

What I tried to do exactly was exactly in the lines of this of the following post:

Using VB.NET IIF I get NullReferenceException

However, the solution I had implemented was borrowed from the following msdn site:

http://msdn.microsoft.com/en-us/library/27ydhh0d%28v=vs.80%29.aspx

Its only after that I learned about the If() method in vb.net.

So in the end I had written a function which returns the appropriate value (which used reflection) to achieve the purpose. But that method (which I wrote) didn't throw any exception. In fact I was able to check for nullability of the type inside the function.

Function ReturnValue(ByVal开发者_如何学运维 MyType As SomeType, ByVal PropertyName as String) As Object
    If MyType Is Nothing Then Return String.Empty
    Dim arrPropInfo As PropertyInfo() = MyType.GetType().GetProperties()
    Return arrPropInfo.Where(Function(x) x.Name = PropertyName).Item(0).GetValue(MyType, Nothing)
End Function

My question is, is there something written inside Iif() to throw an error like that? - a NullReferenceException

If MyType Is Nothing Then Throw New NullReferenceException()

Or is there something much bigger at work here? So suppose if I wanted to write function like iif, which will throw an error if something in the argument list is null is the above way the only way of doing it?


The distinction is that ?: and If() are operators. IIf() is not an operator; it is a plain old function. You can even implement it yourself:

Public Shared Function IIf(ByVal Expression As Boolean, ByVal TruePart As Object, ByVal FalsePart As Object) As Object
    If Expression Then Return TruePart Else Return FalsePart
End Function

This means that all arguments passed to the function must be evaluated before the function is called, just like any other function. There's nothing inside IIF() that throws an exception — it's just the normal process of evaluating arguments passed to a function.

You also need to note that it's perfectly okay to pass null/Nothing to a function. You saw the exception in your code because the expression you were passing had a null reference that could not be evaluated — say, if you did something like try to use the .Length property of a null string variable.

The If() operator, on the other hand, does not have this problem because it is just that: an operator Moreover, it's an operator that takes advantage of short-circuit evaluation, such that only the returned expression needs to be evaluated. If() also has the nice advantage of being type-safe, though you could replicate type-safety in a generic IIF(Of T)() function as well, if you wanted.


IIF differs from C++ :? in at least one significant way, it always evaluates both expressions so you can't do things like IIF(X is nothing,DefaultValue,X.Value). It'll throw a null reference exception because in the case X is nothing, it'll still evaluate X.Value.

IIF works the way it does because it's a function, not an operator

You didn't provide code for how you were using IIF so I'm just guessing. What I just said is the very first comment from the link you provided to MSDN so it's likely you already knew this or this isn't your question.

If your question is really that you want to throw an exception, you should use the code you have above, but throw and ArgumentNullException instead so you can specify which argument was null.


You can add an extra ByRef argument to indicate if indeed there was exception happened in the function. In this way we can eliminate the need to catch any exception raised in the function. Of course, you have to add Try...Catch statement in that function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜