Results 1 to 5 of 5

Thread: [RESOLVED] Loop through Listitems not working

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2021
    Posts
    57

    Resolved [RESOLVED] Loop through Listitems not working

    I load directory paths in a List of strings (YearsList) and then want to retrieve the items in this list so I can loop through these list items when I need them.
    The problem is that the loop doesn't work. It keeps returning the same item.

    Code:
           Dim row As DataRow
            Dim column As DataColumn
            For Each row In dt2.Rows
                For Each column In dt2.Columns
                    MyPath.Path = row(column)
                    YearsList.Add(MyPath)
                Next
            Next
    Code:
           Try
                hOOters = New RaptorDB.Hoot(IndexFolder, "index", True)
                'hOOters = New RaptorDB.Hoot(SearchFolder, "index", True)
                hOOters.FreeMemory() ' will free bitmap memory
                If YearsList.Count > 0 Then
    
                    For Each item In YearsList
                        SearchFolder = item.ToString
                        If (Not IO.Directory.Exists(SearchFolder)) Then
                            MsgBox("Folder " + SearchFolder + " not found!",, Title)
                        Else
                            BackgroundWorker1.RunWorkerAsync()
                        End If
                    Next item
                End If
    
    
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

  2. #2
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,329

    Re: Loop through Listitems not working

    Quote Originally Posted by clausowitz View Post
    I load directory paths in a List of strings (YearsList) and then want to retrieve the items in this list so I can loop through these list items when I need them.
    The problem is that the loop doesn't work. It keeps returning the same item.

    Code:
           Dim row As DataRow
            Dim column As DataColumn
    
            For Each row In dt2.Rows
                For Each column In dt2.Columns
                    MyPath.Path = row(column)
                    YearsList.Add(MyPath)
                Next
            Next
    You repeatedly add the MyPath object to the YearsList list without ever creating a new MyPath object. So you are storing the same object every time through the loop, so every item in the list will be the same item (a reference to the same MyPath object).

    Maybe what you meant was:
    Code:
    YearsList.Add(MyPath.Path)
    That begs the question, what type is MyPath? It can't be String because the String type does not have a Path member. So you shouldn't be able to store MyPaths in a List(Of String). It looks like you are working with Option Strict turned off. You should turn it on now, as advised by jmc in your previous thread.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,687

    Re: Loop through Listitems not working

    The whole point of OOP is the first O. Programming objects are based on real-world objects. Let's say that you were creating a list of people by writing their names on a piece of paper. Let's say that I present myself to you wearing a red shirt and you wrote my name down. Later on, I change my shirt to a green one and present myself again, so you write my name down again. Later still, I present myself to you wearing a blue shirt and you write my name down. If you then went through the list of people on paper and asked them to present themselves to you one by one, what would you expect? You'd get me wearing a blue shirt - the last shirt I put on - three times, right? That's obvious, but that's exactly what you're doing in your code and expecting a different result. If you expect to get three different people wearing three different shirts out of the list, you need to put three different people wearing three different shirts into the list in the first place, not one person wearing one of three different shirts at a time.

  4. #4

    Thread Starter
    Member
    Join Date
    Apr 2021
    Posts
    57

    Re: Loop through Listitems not working

    Solved my problem with the following code:

    Code:
            Dim row As DataRow
            Dim column As DataColumn
            For Each row In dt2.Rows
                For Each column In dt2.Columns
                    MyPath.Path = row(column)
                    YearsList.Add(New MyYears() With {
                    .Path = MyPath.ToString
                })
                Next
            Next

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,687

    Re: Loop through Listitems not working

    Quote Originally Posted by clausowitz View Post
    Solved my problem with the following code:

    Code:
            Dim row As DataRow
            Dim column As DataColumn
            For Each row In dt2.Rows
                For Each column In dt2.Columns
                    MyPath.Path = row(column)
                    YearsList.Add(New MyYears() With {
                    .Path = MyPath.ToString
                })
                Next
            Next
    I don't see the point of MyPath at all.
    vb.net Code:
    1. For Each row As DataRow In dt2.Rows
    2.     For Each column As DataColumn In dt2.Columns
    3.         YearsList.Add(New MyYears With {.Path = CStr(row(column))})
    4.     Next
    5. Next

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width