Mech_Engineer
Mech_Engineer

Reputation: 555

Search table with x number of arguments

I'm trying to find a desired row index of a table(s). Difficulty is that I need to check multiple values found in the columns.

For example.

I would like my function to search the number of given arguments for the columns. But I don't quite know how.

I was thinking of passing a array {"Value1", "Value2", "Value3"}

But how can I check based on the number of arguments if the row matches?

I was thinking somthing like this, but maybe there is a better solution to this problem?

Private Function FindRow(ByVal StringArray As String()) As Integer
    Dim NumberOfArguments As Integer = oStringArray.Length
    Dim MatchesCount As Integer = 0

    For i = 0 as integer to rows.count

        For x = 0 as integer to columns.count
            For y = 0 as integer to NumberOfArguments
                If Row(i).Column(x).value = StringArray(y) Then
                    MatchesCount += 1
                End If
            Next
        Next

        If MatchesCount = NumberOfArguments Then
            FindRow = i
        End If
    Next
End Function

Upvotes: 0

Views: 32

Answers (1)

jmcilhinney
jmcilhinney

Reputation: 54487

Private Function FindRowIndex(ParamArray values As String()) As Integer
    For i = 0 To rows.Count - 1
        Dim row = rows(i)
        Dim match = True

        For j = 0 To values.Length - 1
            If Not row(j).Equals(values(j)) Then
                match = False
                Exit For
            End If
        Next

        If match Then
            Return i
        Next
    Next

    Return -1
End Function

Because the method parameter is declared ParamArray, you can call it and pass multiple discrete values rather than having to create an array explicitly.

Upvotes: 1

Related Questions