473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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
line:

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 & ")"

Next

'This loop Works

For x = 1 To oUser.Groups.Count

group = oUser.Groups.Item(x)

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

Me.TextBox1.Text = var

Next

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)

mCol.Remove(index)

End Sub

'REMOVE Method

Public Sub Remove(ByVal sKey As String)

mCol.Remove(sKey)

End Sub

'ITEM Property

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

Get

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

Get

If bHandleNotExists Then

Try

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

Catch ex As Exception

Return Nothing

End Try

Else

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

End If

End Get

End Property

'COUNT Method

Public ReadOnly Property Count() As Integer

Get

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
mnasecurityGroups

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)

oConn.Open()

'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

Else

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

AllGroups.Add(oGroup)

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

oUserGroups.Add(oGroup)

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.
May 4 '06 #1
3 1634
Or create your class and implement the IEnumerable interface so that it
works with For Each.

May 4 '06 #2
Matt,

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
done.

Cor

"Matt" <ma******@newsgroups.nospam> schreef in bericht
news:%2****************@TK2MSFTNGP02.phx.gbl...


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 line:

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 & ")"

Next

'This loop Works

For x = 1 To oUser.Groups.Count

group = oUser.Groups.Item(x)

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

Me.TextBox1.Text = var

Next

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)

mCol.Remove(index)

End Sub

'REMOVE Method

Public Sub Remove(ByVal sKey As String)

mCol.Remove(sKey)

End Sub

'ITEM Property

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

Get

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

Get

If bHandleNotExists Then

Try

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

Catch ex As Exception

Return Nothing

End Try

Else

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

End If

End Get

End Property

'COUNT Method

Public ReadOnly Property Count() As Integer

Get

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
mnasecurityGroups

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)

oConn.Open()

'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

Else

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

AllGroups.Add(oGroup)

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

oUserGroups.Add(oGroup)

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.

May 5 '06 #3
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.

/claes

"Matt" <ma******@newsgroups.nospam> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Here is the mnasecurityGroups collection class. I think this is where my
problem resides.

Public Class mnasecurityGroups

Private mCol As New Collection


May 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ken Fine | last post by:
I'm trying to include a list of people that's the result of looping through a recordset in a CDONTS mail. I'm trying to Dim the output of a loop, and it ain't working -- I'm getting a syntax error....
8
by: Drew | last post by:
I am building an application for keeping track of user permissions here at work. I have built the interfaces, and am now working on the processing page for inserting to the database. I am having...
8
by: Hardrock | last post by:
I encountered some difficulty in implementing dynamic loop nesting. I.e. the number of nesting in a for(...) loop is determined at run time. For example void f(int n) { For(i=0; i<=K; i++)...
5
by: L. Oborne | last post by:
I have this code working fine in Classic ASP but I get compile errors when I try to run it as ASP.NET. Do While NOT RS.EOF If ... Then... Else... End If RS.MoveNext Loop
34
by: Frederick Gotham | last post by:
Is the domestic usage of the C "for" loop inefficient when it comes to simple incrementation? Here's a very simple program that prints out the bit-numbers in a byte. #include <stdio.h> #include...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
2
by: mrjoka | last post by:
hi experts, i'm developing a page in ASP but i'm doing also some javascript insode the page. i'm creating a frame and i want to loop this frame with a duplicateloop function so the form will be...
2
by: d3vkit | last post by:
Okay so I can NOT get my while loop to work. It's the most confusing thing I've ever come across. It was working fine and then suddenly, nothing. No error. The page just dies. I am using PHP5 with...
6
by: uche | last post by:
This function that I have implemented gives me an infinite loop. I am trying to produce a hexdum program, however, this function is not functioning correctly.....Please help. void...
4
by: joaotsetsemoita | last post by:
hello everyone. Im trying to time out a loot after a certain time. Probably 5 to 10 minutes. I have the following function Private Sub processFileCreation(ByVal source As Object, ByVal e As...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.