Results 1 to 5 of 5

Thread: Updating an arraylist using a For Each Loop

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Updating an arraylist using a For Each Loop

    Hi All,

    I can do this with a for loop, but I was just curious if an arraylist can be updated using a for each loop. ie

    Code:
          For Each arrItem As String In myArrayList
    
                    arrItem = "My Stuff"
    
            Next
    If I loop through the arraylist again the values remain unchanged. Am I missing something here?

    Thanks,

    Strick

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Updating an arraylist using a For Each Loop

    basically arrItem in your For-Each statement is a variable which if changed does not change the original value.

    You would use a For/Next to index elements
    Code:
    Dim Values As New ArrayList
    Values.Add("First")
    Values.Add("Second")
    
    Dim Temp As String = ""
    For row As Integer = 0 To Values.Count - 1
        Temp = Values(row).ToString
        Values(row) = Temp & " !!!"
    Next
    
    For Each item In Values
        Console.WriteLine(item)
    Next
    MSDN http://msdn.microsoft.com/en-us/library/5ebk1751.aspx

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Updating an arraylist using a For Each Loop

    Unless you are working with VS2003 or earlier, you shouldn't be using ArrayLists at all. That alone might be causing the trouble, because an ArrayList is a list of objects. It would be entirely possible for that arraylist to be holding pretty nearly anything, including holding only one item in many different places, though that doesn't seem to be quite the situation here.

    EDIT: The replacement for ArrayList for use with 2005 or later, is List(of T). In your case, if the list holds strings, that would mean a List(of String). Try it, you'll like it.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Re: Updating an arraylist using a For Each Loop

    Thanks guys. I'll try these out when I get to work.

    Strick

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Updating an arraylist using a For Each Loop

    It may still not work... since strings are immutable, your variable that holds the string in the foreach, is a copy of the string from the list. So what you end up changing is a copy of the string... not the list itself. But admitedly I don't use List(of String) very often... I'm usually dealing with a list of objects, which are reference based...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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