Solved

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

Posted on 2013-06-14
2
381 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

19 Experts available now in Live!

Get 1:1 Help Now