我已经编写了自己的方法来以编程方式选择 ComboBox 中的项目:
Function SelectItem(ByVal item As Object, ByVal comboBox As ComboBox) As Boolean
If Not comboBox.Items.Contains(item) Then
comboBox.Items.Add(item)
End If
comboBox.SelectedItem = item
Return True
End Function
"item" 参数可以是任意Class
的,例如字符串,但也可以是 (custom) Structure
。
当参数为Nothing
(或默认结构值)时,此方法应返回False
. 我如何达到这个条件?
' This will not work, because "=" can't be used with classes
If item = Nothing Then Return False
' Won't work either, because "Is" is always False with structures
If item Is Nothing Then Return False
' Obviously this would never work
If item.Equals(Nothing) Then Return False
' Tried this too, but no luck :(
If Nothing.Equals(item) Then Return False
我应该如何处理这种情况?我可以使用Try ... Catch
,但我知道必须有更好的方法。