我希望以下vb.net函数返回一个值Nothing,但它返回一个值为0...
Public Shared Function GetDefaultTipoSrvTkt() As Integer?
Dim tsrvDict As New Dictionary(Of Integer, DataRow)
GetDefaultTipoSrvTkt = If(IsNothing(tsrvDict) OrElse tsrvDict.Count = 0, Nothing, tsrvDict.First.Key)
End Function
Run Code Online (Sandbox Code Playgroud)
该函数的最后一行也可以写成Return If(IsNothing(tsrvDict) OrElse tsrvDict.Count = 0, Nothing, tsrvDict.First.Key)但无论如何,为什么IF()函数If(IsNothing(tsrvDict) OrElse tsrvDict.Count = 0, Nothing, tsrvDict.First.Key)返回0而不是Nothing?
一直在看很多关于使用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
Run Code Online (Sandbox Code Playgroud)
我的问题是,是否有一些内容写在Iif()内来抛出这样的错误? - NullReferenceException
If MyType Is Nothing Then Throw New NullReferenceException()
Run Code Online (Sandbox Code Playgroud)
或者这里有更大的工作吗?所以假设我想编写像iif这样的函数,如果参数列表中的某些内容为null,则抛出错误是上述方法的唯一方法吗?
我正在开发一个新的ASP.Net 4.0数据驱动的应用程序.在DAL中,我检查所有数据的NULL值,并在NULL时默认为正确的数据.我有一个奇怪的问题.两个日期回来了 - 一个我只需要时间.完整日期的第一行代码可以正常工作 - 但第二行代码错误指向格式字符串,但奇怪的是它在NULL值上出错,它不使用格式字符串而只返回Date.MinValue.当第二行获取数据时,它会正确格式化返回.
Dim dr As DataRow
.TourDate = IIf(dr.IsNull("tourdate"), Date.MinValue, Format(dr("tourdate"), "MM/dd/yyyy"))
.TourTime = IIf(dr.IsNull("tourtime"), Date.MinValue, Format(dr("tourtime"), "T"))
Run Code Online (Sandbox Code Playgroud)
当dr("tourtime")为NULL时,错误出现在第二行 - 错误是:参数'Expression'不是有效值.
以下作品:
If 1=1
rdoYes.checked = True
Else
rdoNo.checked = True
End If
Run Code Online (Sandbox Code Playgroud)
但是,以下不起作用:
IIF(1=1, rdoYes.checked = True, rdoNo.checked = True)
Run Code Online (Sandbox Code Playgroud)
为什么是这样?
谢谢!