Solved

How to find the Nth instance of an item in an arraylist?

Posted on 2013-06-14
2
380 Views
Last Modified: 2013-06-17
I need a function that takes the item being searched for, the arraylist being searched and the number of the instance and returns the position of the instance.
0
Comment
Question by:NevSoFly
2 Comments
 
LVL 14

Accepted Solution

by:
quizwedge earned 500 total points
Comment Utility
Returns the index unless it can't find it, in which case it returns -1

 Private Function ReturnNthInstance(ByVal SearchFor As String, ByVal SearchList As ArrayList, ByVal NumberOfInstances As Integer) As Integer
        Dim Counter As Integer = 0
        Dim i As Integer
        For i = 0 To SearchList.Count - 1
            If CStr(SearchList(i)) = SearchFor Then
                Counter += 1
            End If

            If Counter = NumberOfInstances Then
                Return i
            End If
        Next

        Return -1
    End Function

Open in new window

0
 

Author Closing Comment

by:NevSoFly
Comment Utility
perfect.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now