M
Matt
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.
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.