?
Solved

Quick 500 - Enumerate through a Collection using the For Each method?

Posted on 2005-03-04
6
Medium Priority
?
383 Views
Last Modified: 2008-02-01
Hi,

I'm still quite new to .Net but i know this is possible, Could someone convert some simple example code that i've written (below) so that the Form1_Load enumerates through every item in the Obj_Coll Collection - I've partly done it so that it's obvious what i need, just need someone to get it to work!

From reading i know that you have to implement IEnumerable but the CollectionBase hase that right?

I need a nice quick example of this to see how it's some?


Simple Code:
'This pastes right into a basic form btw
Private OBJ_COLL As New gCollection

    Public Class gCollection
        Inherits Collections.CollectionBase

        Public Sub ADD(ByVal Obj As gCollection_Item)
            MyBase.List.Add(Obj)
        End Sub

        Public Function ItemsCollection() As gCollection_Item
            '???? what do i return
            Return MyBase.GetEnumerator
        End Function

        Public Class gCollection_Item
            Dim sName As String
            Property NAme() As String
                Get
                    Return sName
                End Get
                Set(ByVal Value As String)
                    sName = Value
                End Set
            End Property

        End Class
    End Class


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim o As gCollection.gCollection_Item
        Dim i As Long
        For i = 1 To 20
            o = New gCollection.gCollection_Item
            o.NAme = i
            OBJ_COLL.ADD(o)
        Next

        For Each o In OBJ_COLL.ItemsCollection
            MsgBox(o.NAme)
        Next
    End Sub

'Thanks for your Help!
0
Comment
Question by:Cloud9_User
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 25

Assisted Solution

by:RonaldBiemans
RonaldBiemans earned 1000 total points
ID: 13459161
Maybe I'm not understanding this correctly but can't you just do

Private OBJ_COLL As New gCollection

    Public Class gCollection
        Inherits Collections.CollectionBase

        Public Sub ADD(ByVal Obj As gCollection_Item)
            MyBase.List.Add(Obj)
        End Sub

        Public Class gCollection_Item
            Dim sName As String
            Property NAme() As String
                Get
                    Return sName
                End Get
                Set(ByVal Value As String)
                    sName = Value
                End Set
            End Property

        End Class
    End Class


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim o As gCollection.gCollection_Item
        Dim i As Long
        For i = 1 To 20
            o = New gCollection.gCollection_Item
            o.NAme = i
            OBJ_COLL.ADD(o)
        Next

        For Each o In OBJ_COLL
            MsgBox(o.NAme)
        Next
    End Sub
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 13459168
Hi Cloud9_User,

You don't need to do anything to get it to work with For..Each:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionscollectionbaseclasstopic.asp

Your code simply needs to be:

    Private OBJ_COLL As New gCollection

    Public Class gCollection
        Inherits Collections.CollectionBase

        Public Sub ADD(ByVal Obj As gCollection_Item)
            MyBase.List.Add(Obj)
        End Sub

        Public Class gCollection_Item
            Dim sName As String
            Property NAme() As String
                Get
                    Return sName
                End Get
                Set(ByVal Value As String)
                    sName = Value
                End Set
            End Property

        End Class
    End Class


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim o As gCollection.gCollection_Item
        Dim i As Long
        For i = 1 To 20
            o = New gCollection.gCollection_Item
            o.NAme = i
            OBJ_COLL.ADD(o)
        Next

        For Each o In OBJ_COLL
            MsgBox(o.NAme)
        Next
    End Sub
0
 
LVL 25

Expert Comment

by:RonaldBiemans
ID: 13459191
Good afternoon Idle_Mind, seems we came up with the same thing :-)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13459214
Good Morning... (At least it's morning time here.  Gotta get the kids off to school in 15 minutes.)

Hopefully the author will add something and make the question more interesting.    ;)
0
 

Author Comment

by:Cloud9_User
ID: 13459215
LOL

Thats a little wierd.... because my collegue showed me all about enum'ing through a collection and used IEnumerable?!

Now i feel stupid!

Thanks for all your help anyways :)
0
 

Author Comment

by:Cloud9_User
ID: 13459229
Haha... Thanks for the suttle kick Idle_Mind.... Afraid not tho, just another noob making a stupid mistake!

Cheers anyways :P
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question