?
Solved

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

Posted on 2005-03-04
6
Medium Priority
?
386 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
  • 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
Technology Partners: 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…
Suggested Courses

621 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