Search code examples
vb.netcallbyname

Using callbyname to check list.contains(x)


I've been trying to use callbyname to write a generic function which checks if the target list (targetListName) contains a certain item before adding it to the list. Unfortunately, I can't seem to figure out how to use .contains with callbyname. Appreciate any help!

This is the code I'm using now. Supply and Demand are both lists(of string).

Public Sub addItem(ByVal item As String, ByVal targetListName As String)
    Select Case targetListName.ToLower
        Case "supply"
            If supply.Contains(item) = False Then supply.Add(targetListName)
        Case "demand"
            If demand.Contains(item) = False Then supply.Add(targetListName)
        Case Else
            'bugcatch
    End Select
End Sub

I would like to ideally use something like this instead:

Public Sub addItem(ByVal item As String, ByVal targetListName As String)
    If CallByName(Me, targetListName, [Method]).Contains(item) = false Then
        CallByName(Me, targetListName, [Set]).Add(item)
    End If
End Sub

Solution

  • You could just have a dictionary of lists with string as your key. Not sure what the real benefit of is of using CallByName. Could you elaborate on that more on the use case and problem you are trying to solve?