Solved

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

Posted on 2013-06-14
2
384 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
ID: 39249485
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
ID: 39253043
perfect.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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