VB.NET: Generic Search Function For A Two Dimension Array

Many a times I store the data from various sources in Arrays for faster performance

Generally these are two dimensional arrays with data and IDs.
I have been using this generic function to search these arrays for data or IDs.
Hope this will be useful for you as you'll not have to write loops everytime you need to search the arrays.
 
Sample code to use this function is included.
 
Regards
Shobhit
 
Sample Code
 
Dim intCtr As Integer
Dim strCriteriaValue As String, strFoundValue As String
Dim intCriteriaIndex As Integer, intValueIndex As Integer
Dim lngLastSearchedIndex As Long
Dim strFinal As String
 
' >> Populate The Array
' >> Sample Data
' >> Index 0 Contains Values: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
' >> Index 1 Contains Values: a, b, c, d, e, f, g, h, i, j, k
' >> Index 2 Contains Values: true, false, true, false.......
ReDim mastrSampleArray(2, 10)
For intCtr = 0 To UBound(mastrSampleArray, 2)
 mastrSampleArray(0, intCtr) = intCtr
 mastrSampleArray(1, intCtr) = Chr(Asc("a") + intCtr)
 If intCtr Mod 2 = 0 Then
  mastrSampleArray(2, intCtr) = "true"
 Else
  mastrSampleArray(2, intCtr) = "false"
 End If
Next
' >> Search The Data - Sample Call
strCriteriaValue = "true" ' >> Search For This Value In Array Elements
intCriteriaIndex = 2  ' >> The Search Value Has To Be Looked In This Array Index Elements
intValueIndex = 1  ' >> The Values To Be Picked Are To Be Picked From This Index
' >> Iterate To Find The Values With Given Criteria In The Array
While fnSearchArray(mastrSampleArray, strCriteriaValue, intCriteriaIndex, intValueIndex, strFoundValue, lngLastSearchedIndex) = True
    strFinal = strFinal & strFoundValue & ", "
End While
' >> Show Found Items
Call MsgBox(strFinal)
Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish