locked
'System.Collections.Generic.List(Of DCTS_CLASS.TripsDetail)' cannot be converted to 'System.Collections.Generic.List(Of DCTS_CLASS.Trips RRS feed

  • Question

  • I am using VB.Net 2010 and I get the following error on Return tripList "System.Collections.Generic.List(Of DCTS_CLASS.TripsDetail)'

    cannot be converted to 'System.Collections.Generic.List(Of DCTS_CLASS.Trips"

    Imports System.Data.SqlClient Public Class TripsDetailDB Public Shared Function GetTripDetails(ByVal TRIP As Integer) As List(Of Trips) Dim tripList As List(Of TripsDetail) = New List(Of TripsDetail) Dim connection As SqlConnection = dctsConnect.GetConnection Dim selectstatement As String = "SELECT TRIP, RIDER_NO, LNAME, FNAME, PROV_CON, APT_TIME, PKUP_TIME , " & "TD_REMARKS, NEW_RIDER, SORT" & "FROM TRIP_DETAILS " & "WHERE TRIP = @TRIP" Dim selectCommand As New SqlCommand(selectstatement, connection) selectCommand.Parameters.AddWithValue("@TRIP", TRIP) Try connection.Open() Dim reader As SqlDataReader = selectCommand.ExecuteReader() Dim tripDetail As TripsDetail Do While reader.Read tripDetail = New TripsDetail tripDetail.TRIP = CInt(reader("TRIP")) tripDetail.RIDER_NO = CInt(reader("RIDER_NO")) tripDetail.LNAME = CStr(reader("LNAME")) tripDetail.FNAME = CStr(reader("FNAME")) tripDetail.PROV_CON = CInt(reader("PROV_CON")) tripDetail.APT_TIME = CDate(reader("APT_TIME")) tripDetail.PKUP_TIME = CDate(reader("PKUP_TIME")) tripDetail.TD_REMARKS = CStr(reader("TD_REMARKS")) tripDetail.NEW_RIDER = CStr(reader("NEW_RIDER")) tripDetail.SORT = CStr(reader("SORT")) tripList.Add(tripDetail) Loop reader.Close() Catch ex As SqlException Throw ex Finally connection.Close() End Try Return tripList "I get ERROR here End Function End Class



    Poppygb

    Thursday, December 13, 2012 4:24 PM

Answers

  • Hi,

    Check the return type. The issue is that the function return type is a list of trips while you are returning a list of trip details. You may want to change the function return type or else explain what you are trying to do...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Thursday, December 13, 2012 4:38 PM
  • Your function is declared as returning a List(Of Trips) but you appear to actually want to return a List(Of TripsDetail) per the name of the function and the value being returned.

    So if the return type is what you really want (that is, you want a List(of TripsDetail)) then change the function declaration to return a List(Of TripsDetail) instead of returning a List(Of Trips):

    Public Shared Function GetTripDetails(ByVal TRIP As Integer) As List(Of TripsDetail)
     


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, December 13, 2012 4:58 PM
    Moderator

All replies

  • Hi,

    Check the return type. The issue is that the function return type is a list of trips while you are returning a list of trip details. You may want to change the function return type or else explain what you are trying to do...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Thursday, December 13, 2012 4:38 PM
  •  Thanks for the quick reply. I am new at vb.net. How do you check the return type?

    Poppygb

    Thursday, December 13, 2012 4:45 PM
  • Your function is declared as returning a List(Of Trips) but you appear to actually want to return a List(Of TripsDetail) per the name of the function and the value being returned.

    So if the return type is what you really want (that is, you want a List(of TripsDetail)) then change the function declaration to return a List(Of TripsDetail) instead of returning a List(Of Trips):

    Public Shared Function GetTripDetails(ByVal TRIP As Integer) As List(Of TripsDetail)
     


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Thursday, December 13, 2012 4:58 PM
    Moderator
  • Inside the body you return a List(Of TripDetails) while in the function declaration you told it should return a List(Of Trip). AS VB can't convert the value you return to the type you pretend it should be, it fails...

    So my guess is that you want :

    Public Shared Function GetTripDetails(ByVal TRIP As Integer) As List(Of TripDetail)

    rather than

    Public Shared Function GetTripDetails(ByVal TRIP As Integer) As List(Of Trip)


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Thursday, December 13, 2012 5:00 PM
  • Thanks that worked

    Poppygb

    Friday, December 14, 2012 4:59 PM