0

I have this structure:

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:

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:

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:

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

4
  • I will answer your question separately but I have three pieces of advice for you. First, that structure should be a class unless you specifically need a structure for unmanaged code. Secondly, those fields should be properties unless, again, you specifically need fields for unmanaged code. Finally, if you're targeting .NET 2.0 or later then you shouldn't be using the ArrayList class at all. Use a List(Of DISPLAYDIRECTORY) instead. Commented Apr 17, 2014 at 1:54
  • yes..i'm in urgent time. so i post it to my other site also. If you can guide me, please. Commented Apr 17, 2014 at 1:54
  • @jmcilhinney I'm using VS2010. I don't want to query again to search for it because all data is in arraylist. So how to do it? Commented Apr 17, 2014 at 2:02
  • That is completely nonsensical. Where did I ever imply that you would need to perform another query? I even stated that my comment was not an answer to your question but I would be answering it separately, so what is your comment even for? It's like people don;t even read what's in front of them! "I want help but I'm not actually going to read the words you type". Commented Apr 17, 2014 at 2:06

1 Answer 1

1

You should not be using IndexOf at all. The whole point of a For Each loop is to access the items themselves and you then want to get the item that has a particular value for a particular field/property.

Private Function GetMyDetails(ByVal strDirName As String) As DISPLAYDIRECTORY
    Dim obj As DISPLAYDIRECTORY

    Try
        If strDirName <> String.Empty Then
            For Each item As DISPLAYDIRECTORY In arrDirectory
                If item.strdirname = strDirName Then
                    obj = item
                    Exit For
                End If
            Next
        End If
    Catch ex As Exception

    End Try

    Return obj
End Sub
Sign up to request clarification or add additional context in comments.

5 Comments

I'll try it jmcilhinney..I'll get back to ya
It goes to exception: Specified cast is not valid. It goes after stepping to For Each item As DISPLAYDIRECTORY In arrDirectory
Is that a compilation error or a run time exception? I would think that that code should compile so, if it's a run time exception, that means that you have put something into that collection that is not that type.
So a run time exception then. That means that at least one item in your collection is not that type. What type is that item and why is it not the type it apparently should be? If you were using a List(Of T) then you couldn't have this problem because, while the ArrayList can store objects of any type, a generic List can only store items of the type you specify when you create it. That's the main reason that ArrayLists should no longer be used.
I will learn how to use list of(t) in future but right now, I have to use the arraylsit since my project now is urgent..I've been told in another forumn that I better use list of (T) and reason; as you told me.. Thanks man

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.