在 VB.NET 中检查空值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27006157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check for Null Value in VB.NET
提问by Ben Blackmore
I have a function that searches AD for an attribute (extensionAttribute3). It works fine if the attribute has a value, but if it's not set, it errors with 'Object reference not set to an instance of an object.' This always errors on the line:
我有一个在 AD 中搜索属性的函数 (extensionAttribute3)。如果该属性具有值,则它可以正常工作,但如果未设置,则会出现“未将对象引用设置为对象实例”的错误。这总是在线错误:
CurrentPIN = dirResult.GetDirectoryEntry.Properties("extensionAttribute3").Value.ToString
I've tried using "If IsNothing" and "IsNot Nothing" to check for NULL values on both dirResult & CurrentPIN, but it still errors. How can I check for NULL values successfully?
我尝试使用“If IsNothing”和“IsNot Nothing”来检查 dirResult 和 CurrentPIN 上的 NULL 值,但它仍然出错。如何成功检查 NULL 值?
Checking if dirResult is NULL:
检查 dirResult 是否为 NULL:
Private Function GetUserProperties()
Dim ADName As String = GetLogonName()
Dim CurrentPIN As String = Nothing
Dim dirEntry As DirectoryEntry = GetDirectoryEntry()
Dim dirSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)
dirSearcher.Filter = ("(samAccountName=" & ADName & ")")
dirSearcher.PropertiesToLoad.Add("extensionAttribute3")
dirSearcher.SearchScope = SearchScope.Subtree
Dim dirResult As SearchResult = dirSearcher.FindOne()
If IsNothing(dirResult) Then
Return "<not set>"
Else
CurrentPIN = dirResult.GetDirectoryEntry.Properties("extensionAttribute3").Value.ToString
Return CurrentPIN
End If
End Function
Checking if CurrentPIN is NULL:
检查 CurrentPIN 是否为 NULL:
Private Function GetUserProperties()
Dim ADName As String = GetLogonName()
Dim CurrentPIN As String = Nothing
Dim dirEntry As DirectoryEntry = GetDirectoryEntry()
Dim dirSearcher As DirectorySearcher = New DirectorySearcher(dirEntry)
dirSearcher.Filter = ("(samAccountName=" & ADName & ")")
dirSearcher.PropertiesToLoad.Add("extensionAttribute3")
dirSearcher.SearchScope = SearchScope.Subtree
Dim dirResult As SearchResult = dirSearcher.FindOne()
CurrentPIN = dirResult.GetDirectoryEntry.Properties("extensionAttribute3").Value.ToString
If IsNothing(CurrentPIN) Then
Return False
Else
Return CurrentPIN
End If
End Function
回答by Josh Part
You should check for NULL in Properties("extensionAttribute3"):
您应该在 Properties("extensionAttribute3") 中检查 NULL:
If dirResult Is Nothing OrElse dirResult.GetDirectoryEntry.Properties("extensionAttribute3") Is Nothing Then
Return "<not set>"
Else
CurrentPIN = dirResult.GetDirectoryEntry.Properties("extensionAttribute3").Value.ToString
Return CurrentPIN
End If
Explanation:
解释:
Is Nothingis IMO a cleaner way to check for NULLs, and AFAIK it also saves you a stack level.
Is NothingIMO 是一种更干净的检查 NULL 的方法,AFAIK 它还为您节省了一个堆栈级别。
If extensionAttribute3 doesn't have a value, you are having the exception because
如果 extensionAttribute3 没有值,则会出现异常,因为
CurrentPIN = dirResult.GetDirectoryEntry.Properties("extensionAttribute3").Value.ToString
CurrentPIN = dirResult.GetDirectoryEntry.Properties("extensionAttribute3").Value.ToString
is translated to
被翻译成
CurrentPIN = <NULL>.Value.ToString
CurrentPIN = <NULL>.Value.ToString
Checking for NULL on dirResult is just to prevent a possible future exception.
在 dirResult 上检查 NULL 只是为了防止将来可能发生的异常。

