Unexpected Cast error in 'For Each'
Hi,
I'm converting some VB6 code to .NET and I'm having an error occur when I try to loop through a collection of objects:
'An unhandled exception of type 'System.InvalidCastException' occurred in SiD.exe
Additional information: Specified cast is not valid.'
It basically doesn't like the 'For Each' statement in the simplified code listing below:
Code:
Sub PopTreeView()
{ Other Declarations }
Dim oServer As cServer
For Each oServer In gcolServers
{ Processing Code }
Next
End Sub
The gcolServers variable is a collection of cServers, so its got me confused as to why the cast is invalid. As far as I understand it the enumeration within a collection is inherited in .NET so I don't need to code for it specifically like in VB6. Here's the object and collection definitions (simplified for space) anyhow:
Code:
Public Class cServer
' Local variables to hold property values
'
{Local variable defs}
{Property gets and sets}
Public Sub New(ByVal intServerID As Integer, _
ByVal strServerName As String, _
ByVal strServerDescription As String, _
ByVal intLocationID As Integer, _
ByVal intOSTypeID As Integer, _
ByVal strNotes As String, _
ByVal intModelTypeID As Integer, _
ByVal strSerialNumber As String, _
ByVal strSAPassword As String, _
ByVal strSystemAdminPassword As String, _
ByVal strSQLSSAPassword As String, _
ByVal strMachineAdminPassword As String, _
ByVal clDrives As colDrives, _
ByVal clDatabases As colDatabases, _
ByVal intGroupID As Integer, _
ByVal intGroupOrder As Integer, _
Optional ByVal bIsPwdDirty As Boolean = False, _
Optional ByVal bIsDetDirty As Boolean = False)
ServerID = intServerID
ServerName = strServerName
ServerDescription = strServerDescription
LocationID = intLocationID
OSTypeID = intOSTypeID
Notes = strNotes
ModelTypeID = intModelTypeID
SerialNumber = strSerialNumber
SAPassword = strSAPassword
SystemAdminPassword = strSystemAdminPassword
SQLSSAPassword = strSQLSSAPassword
MachineAdminPassword = strMachineAdminPassword
Drives = Drives
Databases = Databases
GroupID = intGroupID
GroupOrder = intGroupOrder
IsPwdDirty = IsPwdDirty
IsDetDirty = IsDetDirty
End Sub
End Class
Public Class colServers
Inherits System.Collections.DictionaryBase
Public Function Add(ByVal ServerID As Integer, _
ByVal ServerName As String, _
ByVal ServerDescription As String, _
ByVal LocationID As Integer, _
ByVal OSTypeID As Integer, _
ByVal Notes As String, _
ByVal ModelTypeID As Integer, _
ByVal SerialNumber As String, _
ByVal SAPassword As String, _
ByVal SystemAdminPassword As String, _
ByVal SQLSSAPassword As String, _
ByVal MachineAdminPassword As String, _
ByVal Drives As colDrives, _
ByVal Databases As colDatabases, _
ByVal IsDetDirty As Boolean, _
ByVal IsPwdDirty As Boolean, _
ByVal GroupID As Integer, _
ByVal GroupOrder As Integer, _
ByVal sKey As String) As cServer
Dim oServer As cServer
' Add the properties to the collection as a new Server Class
'
dictionary.Add(sKey, New cServer(ServerID, _
ServerName, _
ServerDescription, _
LocationID, _
OSTypeID, _
Notes, _
ModelTypeID, _
SerialNumber, _
SAPassword, _
SystemAdminPassword, _
SQLSSAPassword, _
MachineAdminPassword, _
Drives, _
Databases, _
GroupID, _
GroupOrder, _
IsDetDirty, _
IsPwdDirty))
' Return the object created
'
Return dictionary.Item(sKey)
End Function
Public Sub Remove(ByVal strKey As String)
' remove the cServer Class from the collection for the supplied Key
'
dictionary.Remove(strKey)
End Sub
Default Public Property Item(ByVal strKey As String) As cServer
Get
Return DirectCast(Dictionary.Item(strKey), cServer)
End Get
Set(ByVal Value As cServer)
Me.Dictionary.Item(strKey) = Value
End Set
End Property
Public Sub New()
MyBase.New()
End Sub
End Class
Anyone got any suggestions?
Rob