Reputation: 11399
I am trying to bring my old VB6 code to a modern VB.NET code.
In my VB6 code, I need to query if a key exists in a collection.
I do it like this:
Private Function pIndexFromKey(ByRef uCol As Collection, ByVal uText As String) As Long
On Error Resume Next
Dim lRet&
lRet = uCol.Item(uText)
pIndexFromKey = lRet
End Function
If pIndexFromKey returns 0, I know that the key is not contained in the collection, and I add it like this:
nCollection.Add(lIndex, sText)
I am wondering if this is "nice" approach. I think not because in .NET I am using a VisualBasic collection, and the fact that it is "VB" and not a system collection makes me suspicious.
Just for the records, this is my VB.NET code:
Private Function pIndexFromKey(ByRef uCol As Collection, ByVal uText As String) As Integer
On Error Resume Next
Dim lRet As Integer = CInt(uCol(uText))
Return lRet
End Function
The code works fine, but my On Error Resume Next approach looks ugly, and I don't like having the debug window telling me about the exception each time the error is thrown (and eaten).
Does anybody have any better ideas?
Upvotes: 0
Views: 337
Reputation: 26424
You could use a Generic.Dictionary(Of String, Integer). So instead of this:
Private Function pIndexFromKey(ByRef uCol As Collection, ByVal uText As String) As Integer
On Error Resume Next
Dim lRet As Integer = CInt(uCol(uText))
Return lRet
End Function
You would have this:
Private Function pIndexFromKey(dict As Dictionary(Of String, Integer), uText As String) As Integer
Dim lRet As Integer
If dict.TryGetValue(uText, lRet) Then Return lRet
Return -1 'default value if key was not found in the dictionary
End Function
Upvotes: 1
Reputation: 216293
Drop your VB collection and use the advanced Generic List.
In your case I suspect you are using a simple List(Of String).
If so, use this replacement for your method
Dim k as List(Of String) = new List(Of String)
k.Add("Test1")
k.Add("Test2")
k.Add("Test3")
k.Add("Test4")
k.Add("Test5")
' No need to use Contains, IndexOf doesn't throw exceptions if the element is not there
Dim x = k.IndexOf("Test4")
if x = -1 then
Console.WriteLine("Test4 is not in list")
else
Console.WriteLine("Test4 is at index" + x.ToString)
End if
Upvotes: 1
Reputation: 14088
I'd replace the VisualBasic.Collection
with the ObjectModel.Collection(Of T)
for starters. Then, get rid of your custom function and just check the Contains()
method.
Dim nCollection As New ObjectModel.Collection(Of String)
Dim sText As String = "value"
If Not nCollection.Contains(sText) Then
nCollection.Add(uText)
End If
If nCollection.Contains(sText) Then
Dim index = nCollection.IndexOf(sText)
End If
Upvotes: 0
Reputation: 502
I wouldn't use " on errer resume next" approach. just Test the collection using "contains" method.
Dim Ret as integer
Ret=0
If (uCol.contains(uText)) then
Ret= CInt(uCol(uText))
Return ret
Upvotes: 1
Reputation: 1
If you have Vb.Net that you can handle the error in the Try use Example: Try
Catch ex As Exception
End Try
I hope I understand correctly
Upvotes: 0