Click here to Skip to main content
15,915,864 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

I have this structure:

VB
Public strcMyInfo As DISPLAYDIRECTORY
Public arrDirectory As ArrayList

Public Structure DISPLAYDIRECTORY
    Dim strdirno As String
    Dim strdirname As String
    Dim strdirdetails As String
    Dim strcategory As String
    Dim strwebsite As String
    Dim strphoneno As String
End Structure

I will do a query to database and add structure strcMyInfo to arrDirectory. The arrDirectory will hold data which contains the strcMyInfo data let's say 10 index. For example, the value of arrDirectory.Item(6).strdirname is G2000. How can I loop through the arraylist to find the value of G2000 together with strdirno, strdirdetails,strcategory,strwebsite and strphoneno?

I have search for internet but they only look for a 1 value when adding as example below:
VB
myAL.Add("the")
myAL.Add("quick")
myAL.Add("brown")
myAL.Add("fox")
myAL.Add("jumps")
myAL.Add("over")
myAL.Add("the")
myAL.Add("lazy")
myAL.Add("dog")


But my code will be like this:
VB
If (rdr.HasRows()) Then
     arrDirectory = Nothing
     arrDirectory = New ArrayList
     While rdr.Read
          With strcSearchDir
                  If Not IsDBNull("dirno") Then
                            .strdirno = (rdr("dirno"))
                  Else
                            .strdirno = "N/A"
                  End If
                  If Not IsDBNull("dirname") Then
                            .strdirname = (rdr("dirname"))
                  Else
                            .strdirname = "N/A"
                  End If
                  If Not IsDBNull("dirdetails") Then
                            .strdirdetails = (rdr("dirdetails"))
                  Else
                            .strdirdetails = "N/A"
                  End If
                  If Not IsDBNull("category") Then
                            .strcategory = (rdr("category"))
                  Else
                            .strcategory = "N/A"
                  End If
                  If Not IsDBNull("website") Then
                            .strwebsite = (rdr("website"))
                  Else
                            .strwebsite = "N/A"
                  End If
                  If Not IsDBNull("phoneno") Then
                            .strphoneno = (rdr("phoneno"))
                  Else
                            .strphoneno = "N/A"
                  End If
          End With
          arrDirectory.Add(strcSearchDir)
     End While
     Return True
Else
     Return False
End If


Below are code to find the string but it stop there because i don't know how to continue:

VB
Private Sub GetMyDetails(ByVal strLabel As Label)
        Dim obj As Object
        Try
            If strLabel.Content <> String.Empty Then
                For Each obj In arrDirectory
                    If arrDirectory.IndexOf(obj) = strLabel.Content Then

                    End If
                Next
            End If
        Catch ex As Exception

        End Try
End Sub


If someone know how to use the indexof in arraylist, please guide me. Thanks
Posted

1 solution

Never ever use ArrayList. This class was rendered obsolete as early as of .NET v.2.0, with introduction of generics. If is not formally marked as obsolete only because it is good enough to leave it in some legacy code. Its potential problem is a need for type casts, an extra source of possible bugs (why do you thing generics were introduced for?).

Instead, use the generic class System.Collections.Generic.List<>:
http://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx[^].

For search, you can use the methods named Find or FindIndex, using a predicate parameter, so you can search by any search criteria:
http://msdn.microsoft.com/en-us/library/0k601hd9%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/x0b5b5bc%28v=vs.110%29.aspx[^].

The MSDN documentation referenced above is crystal clear, so I hope you can solve your problem with search easily.

Naturally, the time complexity of such search is O(N) (linear). You can have complexity of O(1) (asymptotically, not depending on the size of data), with collections based on backets with indexing by some key (which you would need to create based on your search criteria):
http://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/ms132319%28v=vs.110%29.aspx[^].

Please see: http://en.wikipedia.org/wiki/Big_O_notation[^].

I'm not explaining the part related to key-indexed collection because this is not a part of the question, but you better learn these types, as well as all the collections, as knowing them is very important for your work.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900