Help with FindIndex method; using predicate.

I have this bit of code, below, that finds the index of the first TextElement in eles that contains the dataValue.    I want to modify it so I can pass a count and find the Nth occurrence of dataValue.

Thanks

Dim eles As New List(Of TextElement)

dim dataValue as string = 'george washington'

index = eles.FindIndex(Function(dataValue As TextElement)
                                                    Return dataValue.value = dataValue
                                        End Function)


 Private Class TextElement

        Public value As String
        Public element As XElement

    End Class
LVL 1
HLRosenbergerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi  HLRosenberger;

Try it like this.

'' Do not use a variable name the same as the variable name as in the Predicate
Dim indexOfString As String = "george washington"

Dim index = eles.FindIndex(Function(dataValue As TextElement)
                               Return dataValue.value = indexOfString
                           End Function)

Open in new window

0
HLRosenbergerAuthor Commented:
I understand that I should change that variable name.   However, how does changing that variable name make is so that I can find the Nth occurrence of data?
0
Fernando SotoRetiredCommented:
Hi  HLRosenberger;

This should give you what you need.
Dim indexOfString As String = "george washington"
Dim nthOccurrence = 1         ' Nth occurrence to find 
Dim numberOfOccurences = 0    ' Number of occurrences already found

Dim index = eles.FindIndex(Function(dataValue As TextElement)
                               If dataValue.value = indexOfString Then
                                   numberOfOccurences += 1
                                   If numberOfOccurences = nthOccurrence Then
                                       Return True
                                   Else
                                       Return False
                                   End If
                               End If
                               Return False
                           End Function)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
HLRosenbergerAuthor Commented:
thanks!
0
Fernando SotoRetiredCommented:
Not a problem HLRosenberger, glad to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.