简体   繁体   中英

VB.NET Should I use a collection for this?

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?

您可以简单地使用contains方法来检查密钥是否存在。

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

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM