Results 1 to 5 of 5

Thread: [2005] loop through arraylist [resolved]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Austin
    Posts
    397

    [2005] loop through arraylist [resolved]

    this should be simple enough... what is the correct way to loop through an arraylist?

    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim CorpGroups As ArrayList = Groups.GetGroupNums
    
            PrintValues(CorpGroups)
    
            With Me.DataGridView1
                .DataSource = CorpGroups
            End With
    
        End Sub
    
        Public Shared Sub PrintValues(ByVal myList As IEnumerable)
            Dim myEnumerator As System.Collections.IEnumerator = myList.GetEnumerator()
    
            While myEnumerator.MoveNext()
                Console.Write(ControlChars.Tab + "{0}", myEnumerator.Current)
            End While
    
            Console.WriteLine()
    
        End Sub

    Console.Write outputs this:
    GroupReportsFolderCreator.Groups

    but the GridView has the correct info:
    Code:
    corp_num  group_num
    123            1586
    123            1587
    Last edited by texas; Jul 12th, 2006 at 10:13 AM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] loop through arraylist

    What is the problem, and do you need to use the Enumerator?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Austin
    Posts
    397

    Re: [2005] loop through arraylist

    i'm just trying to loop through the arraylist(both columns).

    and do you need to use the Enumerator?
    i'm open to anything at this point.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] loop through arraylist

    Don't use an enumerator explicitly. Enumerators exist mainly to facilitate the use of For Each loops. That means that you should use a For Each loop. Also, if each item in the ArrayList is an object with two properties then you must refer to those two properties to get their values.:
    VB Code:
    1. For Each item As Groups In myArrayList
    2.     Console.WriteLine(item.CorpNum & ControlChars.Tab & item.GroupNum)
    3. Next item
    Also, you should generally avoid ArrayLists in VB 2005. If you want a collection of Groups objects then you should use a List(Of Groups). The Generic.List class is basically the same as an ArrayList but allows you to specify a type for the items when you declare it, making it strongly typed, unlike the ArrayList that will accept any objects.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2001
    Location
    Austin
    Posts
    397

    Re: [2005] loop through arraylist

    thank you and i'm looking into the Generic.List.

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