I have a class of properties which is being used as part of a web service. The Class has list of another class as one of its properties:

<Serializable()> _
Public Class LessonPlans
    Public Property Teacher As String
    Public Property RoomNumber As String
    Public Property Subject As String
    Public Property Students As List(Of Student)
End Class

<Serializable()> _
Public Class Student
    Public Property FirstName As String
    Public Property LastName As String
End Class

When I work with the LessonPlans class on the local application, I have no problem doing the following:

Dim _NewLesson As New LessonPlans
_NewLesson.RoomNumber = "5"
_NewLesson.Subject = "English"
_NewLesson.Teacher = "Mr Smith"

Dim _Students As New List(Of Student)

Dim _NewStudent As New Student
_NewStudent.FirstName = "James"
_NewStudent.LastName = "Jones"

_Students.Add(_NewStudent)

_NewLesson.Students = _Students

However, when I try to add the following line on the application that consumes the web service:

_NewLesson.Students = _Students

I get an error: Error 6 Value of type 'System.Collections.Generic.List(Of WindowsApplication1.ws_lessons.Students)' cannot be converted to '1-dimensional array of WindowsApplication1.ws_lessons.Students'.

Recommended Answers

All 6 Replies

Try changing your class to have a new instance of List(Of Student)

For example:

<Serializable()> _    
Public Class LessonPlans
    Private lstStudents As New List(Of Student)
    Public Property Teacher As String
    Public Property RoomNumber As String
    Public Property Subject As String
    Public Property Students As List(Of Student)
    Get
        Return Me.lstStudents
    End Get
    Set(value As List(Of Student)
        Me.lstStudents = value
    End Set
End Class

This may be the compilers best way of dealing with an uninstantiated list.

You can simplify thing quite a bit by leveraging the New constructor in your class something like this:

<Serializable()> _
Public Class LessonPlans
    Public Property Teacher As String
    Public Property RoomNumber As String
    Public Property Subject As String
    Public Property Students As New List(Of Student)
End Class
<Serializable()> _
Public Class Student
    Public Property FirstName As String
    Public Property LastName As String
    Public Sub New()
        FirstName = ""
        LastName = ""
    End Sub
    Public Sub New(_firstname As String, _lastname As String)
        FirstName = _firstname
        LastName = _lastname
    End Sub
End Class

Then adding a student can be done directly to the lessonplan list:

    Dim _NewLesson As New LessonPlans
    _NewLesson.RoomNumber = "5"
    _NewLesson.Subject = "English"
    _NewLesson.Teacher = "Mr Smith"
    _NewLesson.Students.Add(New Student("James", "Jones"))
commented: Makes code cleaner! +9

Thanks for the replies.

It seems that even with changing the code with your suggestions, when adding the class through a web reference, the "list" gets lost.

Obviously there must be som limitation with the web reference "import."

After doing a bit more research I found that while there is a rather complicated way of achieving what I need above, it is far easier to work with a Generic List from a web service by importing it into your project as a Service Reference and not as a Web Reference. The Web Reference functionality in the newer versions of Visual Studio is outdated and seems to have been superseded by Service Reference.

glad you found a solution. please remember to mark this solved. thanks.

Done :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.