ArrayList

ConnyD

Member
Joined
Oct 1, 2006
Messages
8
Programming Experience
1-3
Hi all,

I've been banging my head on the desk with this one for over an hour now.

I've created an EmployeeDetails class with the following data members accessed through properties:

EmployeeID
FirstName
LastName
TitleOfCourtesy

There is a single New constructor defined as follows

VB.NET:
Public Sub New(ByVal nEmployee As Integer, ByVal strFirstName As String, _
ByVal strLastName As String, ByVal strTitleOfCourtesy As String)

This is a simple class and I'm happy all is well with it.

My problem is this:

I have developed another EmployeeDB class which handles connection to a database and retrieval of records. The problem I'm having is with the following:

VB.NET:
  Public Function GetEmployees() As EmployeeDetails
        Dim objCon As New SqlConnection(connectionString)
        Dim objCmd As New SqlCommand("GetAllEmployees", objCon)
        objCmd.CommandType = Data.CommandType.StoredProcedure

        Dim Employees As New System.Collections.ArrayList()
        Try
            objCon.Open()
            Dim objReader As SqlDataReader = objCmd.ExecuteReader()
            Do While objReader.Read()
                Dim emp As New EmployeeDetails(CInt(objReader("EmployeeID")), _
                                                        CStr(objReader("FirstName")), _
                                                        CStr(objReader("LastName")), _
                                                        CStr(objReader("TitleOfCourtesy")))
                employees.Add(emp)
            Loop
            objReader.Close()
            Return Employees

        Catch ex As Exception

        End Try

    End Function

I'm Getting the following error

Value of type 'System.Collections.ArrayList' cannot be converted to EmployeeDetails.

I'd be very grateful of someone could shed some light on this

Thankyou
 
Return an array of EmployeeDetails:
VB.NET:
Public Function GetEmployees() As EmployeeDetails()
Use a List(Of T) for EmployeeDetails:
VB.NET:
Dim Employees As New List(Of EmployeeDetails)
Return the typed array:
VB.NET:
Return Employees.ToArray

Post in most appropriate forum, thread moved.
 
Back
Top