我想知道是否可以使用"AndAlso“来避免由对象"nothing”引起的错误,而不需要使用双重“if --然后”,例如,我有以下代码:
Public Sub Main() Handles Btn_Main.Click
Dim Word As String = InputBox("Insert a word", "Word", "Word")
Dim mObject As Object = mWord(Word)
If Not mObject Is Nothing Then
If mObject.length >= 6 Then
MsgBox("The 6th char of the word is " & mObject(6))
Else
MsgBox("the word is shorter than 6 characters")
End If
End If
End Sub
Function mWord(ByVal Word As String) As Object
Dim mReturn As Object = Word.tochararray
If mReturn.length > 6 Then
Return mReturn
Else
Return Nothing
End If
End Function
我能不能避免双重“如果-那么”这样:
Public Sub Main() Handles Btn_Main.Click
Dim Word As String = InputBox("Insert a word", "Word", "Word")
Dim mObject As Object = mWord(Word)
If Not mObject Is Nothing AndAlso mObject.length >= 6 Then
MsgBox("The 6th char of the word is " & mObject(6))
Else
MsgBox("the word is shorter than 6 characters")
End If
End Sub
这是使用AndAlso的好方法吗?
https://stackoverflow.com/questions/33632981
复制