vb.net:iif 如何给出空引用异常?
看了很多关于使用 Iif() 的帖子/文章。所有这一切都始于我尝试在 C/C#/C++ 等中像条件运算符一样使用 IIf() 。
我尝试做的正是以下帖子中的内容:
使用 VB.NET IIF 我得到 NullReferenceException
但是,我实现的解决方案是借用的以下 msdn 站点:
http://msdn .microsoft.com/en-us/library/27ydhh0d%28v=vs.80%29.aspx
之后我才了解了vb.net中的If()方法。
所以最后我写了一个返回适当值(使用反射)的函数来达到目的。但那个方法(我写的)没有抛出任何异常。事实上,我能够检查函数内类型的可为空性。
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
我的问题是, Iif() 内部是否写了一些东西来抛出这样的错误? - NullReferenceException
If MyType Is Nothing Then Throw New NullReferenceException()
或者这里有更大的东西在起作用吗?所以假设如果我想编写像 iif 这样的函数,如果参数列表中的某些内容为空,它将抛出错误,上面的方法是唯一的方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
区别在于
?:
和If()
是运算符。IIf()
不是运算符;这是一个普通的旧函数。您甚至可以自己实现它:这意味着传递给函数的所有参数都必须在调用函数之前进行计算,就像任何其他函数一样。 IIF() 内部没有任何东西会引发异常——这只是评估传递给函数的参数的正常过程。
您还需要注意,将 null/Nothing 传递给函数是完全可以的。您在代码中看到了异常,因为您传递的表达式有一个无法计算的空引用 - 例如,如果您尝试使用空字符串变量的 .Length 属性。
另一方面,If() 运算符不存在此问题,因为它只是:一个运算符 而且,它是一个利用短路求值的运算符,因此只需要对返回的表达式进行求值。 If() 还具有类型安全的优点,不过如果您愿意,也可以在通用 IIF(Of T)() 函数中复制类型安全。
The distinction is that
?:
andIf()
are operators.IIf()
is not an operator; it is a plain old function. You can even implement it yourself: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 与 C++ 不同:?至少以一种重要的方式,它总是对两个表达式求值,因此您不能执行类似
IIF(X is Nothing,DefaultValue,X.Value)
的操作。它会抛出一个空引用异常,因为在 X 是什么的情况下,它仍然会计算 X.Value。IIF 的工作方式是因为它是一个函数,而不是一个运算符
您没有提供如何使用 IIF 的代码,所以我只是猜测。我刚才说的是您向 MSDN 提供的链接中的第一条评论,因此您可能已经知道这一点,或者这不是您的问题。
如果您的问题确实是要引发异常,则应该使用上面的代码,但是 throw 和 ArgumentNullException 相反,这样您就可以指定哪个参数为空。
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.
您可以添加额外的 ByRef 参数来指示函数中是否确实发生了异常。通过这种方式,我们可以消除捕获函数中引发的任何异常的需要。当然,您必须在该函数中添加 Try...Catch 语句。
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.