For Each Loop Not Working



I am in the process of creating a custom .NET object (this is my first one).
I have created a few classes and some of them are collections. When I try to
iterate through a collection I receive the following error on the For each

Expression is of type 'mnaSecurityControl.mnasecurityGroups', which is not a
collection type.

Here is some more code for better troubleshooting.

Windows Form used to access the object:

Dim oUser As mnasecurityUser

Dim oDBAdapter As New mnasecurityDBAdapter

Dim group As mnasecurityGroup

Dim x As Integer

Dim var As String

oUser = oDBAdapter.GetUser("mamarsha")

'GetGroups is a function that is returned as mnasecurityGroups

oUser.Groups = oDBAdapter.GetGroups(oUser)

'This loop does not work and the error is on oUser.Groups

For Each group In oUser.Groups

var = var & "(" & group.Name & ")"


'This loop Works

For x = 1 To oUser.Groups.Count

group = oUser.Groups.Item(x)

var = var & "(" & group.Name & ")"

Me.TextBox1.Text = var


Here is the mnasecurityGroups collection class. I think this is where my
problem resides.

Public Class mnasecurityGroups

Private mCol As New Collection

'ADD Method

Public Sub Add(ByVal oGroup As mnasecurityGroup, Optional ByVal sKey As
String = Nothing)

mCol.Add(oGroup, sKey)

End Sub

'REMOVE Method

Public Sub Remove(ByVal index As Integer)


End Sub

'REMOVE Method

Public Sub Remove(ByVal sKey As String)


End Sub

'ITEM Property

Public ReadOnly Property Item(ByVal index As Integer) As mnasecurityGroup


Return CType(mCol.Item(index), mnasecurityGroup)

End Get

End Property

'ITEM Property

Public ReadOnly Property Item(ByVal sKey As String, Optional ByVal
bHandleNotExists As Boolean = False) As mnasecurityGroup


If bHandleNotExists Then


Return CType(mCol.Item(sKey), mnasecurityGroup)

Catch ex As Exception

Return Nothing

End Try


Return CType(mCol.Item(sKey), mnasecurityGroup)

End If

End Get

End Property

'COUNT Method

Public ReadOnly Property Count() As Integer


Return mCol.Count

End Get

End Property

End Class

Here is the get groups method:

'METHOD - Get User Groups - Accepts User as object

Public Function GetGroups(ByRef oUser As mnasecurityUser) As

Dim oUserGroups As mnasecurityGroups = Nothing 'User Groups Collection

Dim oGroup As mnasecurityGroup = Nothing 'All Groups Collection

'Database Variables

Dim oConn As SqlConnection

Dim oCommand As SqlCommand

Dim oDataReader As SqlDataReader

Dim sSQL As String

'Open Database

oConn = New SqlConnection(sConnStr)


'SQL String

sSQL = "Select a.group_name, a.group_desc, b.group_id " & _

" From groups a inner join user_groups b" & _

" On a.group_id = b.group_id" & _

" Where user_id = '" & oUser.ID & "'"

'Create Command object

oCommand = New SqlCommand(sSQL, oConn)

oCommand.CommandType = CommandType.Text

'Run query and return to reader object (similar to ADO 2.x RecordSet object)

oDataReader = oCommand.ExecuteReader()

'Iterate through Reader object

While oDataReader.Read()

'Check if the group exists withing the Groups collection

If AllGroups Is Nothing Then

AllGroups = New mnasecurityGroups


oGroup = AllGroups.Item(oDataReader("group_id"), True)

End If

If oGroup Is Nothing Then

oGroup = New mnasecurityGroup

'Set Group object properties

oGroup.ID = oDataReader("group_id")

oGroup.Name = oDataReader("group_name").ToString.Trim()

oGroup.Desc = oDataReader("group_desc").ToString.Trim()

'Add the group to the Groups collection


End If

'Check if Groups collection has been instantiated

If oUserGroups Is Nothing Then

oUserGroups = New mnasecurityGroups

End If

'Add Group to User Groups collection


End While

'Return the Groups object

'GetGroups = oUserGroups

Return oUserGroups

End Function

Let me know if you require further information. Please bear with me because
this is my first custom class.


Here is the mnasecurityGroups collection class. I think this is where my
problem resides.

Public Class mnasecurityGroups

Private mCol As New Collection

You need to inherit the collection class. Do a google search on inheritance
and extending objects.

Chris Dunaway

Or create your class and implement the IEnumerable interface so that it
works with For Each.

Cor Ligthert [MVP]


I thought that I have answered this more to you. The way you go is a long
road to go while the answer is so simple.

Take a straight datatable or if you want to make what you are busy with now,
just create a strongly typed dataset with the wizard and look how it is


Claes Bergefall

Jupp, that's where the problem is. Your mnasecurityGroups class isn't a
collection, it only contains a collection member. Your class needs to
inherit IEnumerable, IList and ICollection to work properly as a collection.
Easiest way to do that is by inheriting CollectionBase (if you search for it
in the docs you'll find a howto article).

Take a look at the classes in the System.Collections.Generic namespace too.
Maybe there is some existing collection class that you can use instead of
implementing your own.


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question