I’ve written my own method for programmatically selecting an item in a 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
The “item” parameter can be any Class, like a String, but it can also be a (custom) Structure.
When the parameter is Nothing (or the default structure value), this method should return False. How do I achieve this condition?
' 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
How should I handle this condition? I could use Try ... Catch, but I know there must be a better way.
This function does the trick:
Test results by passing variable: