|
-
Jul 11th, 2006, 03:54 PM
#1
Thread Starter
Hyperactive Member
[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.
-
Jul 11th, 2006, 04:54 PM
#2
Re: [2005] loop through arraylist
What is the problem, and do you need to use the Enumerator?
-
Jul 11th, 2006, 05:04 PM
#3
Thread Starter
Hyperactive Member
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.
-
Jul 11th, 2006, 06:15 PM
#4
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:
For Each item As Groups In myArrayList
Console.WriteLine(item.CorpNum & ControlChars.Tab & item.GroupNum)
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.
-
Jul 12th, 2006, 10:13 AM
#5
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|