views:

530

answers:

2

Within a Web Service CarService,
- I have a class called Car.
- Car has a bunch of properties i.e. Car.Color.
- CarService has a method called GetCars().

- Within GetCar, I have a loop that appends a List of, you guessed it.. Cars.

Dim carList As New List(Of Car)

Do While rdr.Read()
    If rdr.GetValue(1).ToString().Length > 0 Then
        Dim c As Car = New Car
        c.Color = rdr.GetValue(1).ToString()
        carList.Add(c)
    End If
Loop

GetCars() returns carList.
I also have another page within a different project that consumes that data.
Or at least, I'd like to and here's my problem..

If I do..

Dim myNewCars As List(Of Namespace.Car)
myNewCars = CarServiceProxy.GetCars()

I get:

Value of type '1-dimensional array of Namespace.Car' cannot be converted to 'System.Collections.Generic.List(Of Namespace.Car)'.

What would be the best way to convert this 1-dimensional array' back into a List of Cars?

+1  A: 

With .NET 3.5 you could do:

Dim myNewCars As List(Of Namespace.Car)
myNewCars = CarServiceProxy.GetCars().ToList()
Meta-Knight
+3  A: 

Without changing anything? Use the List<T> constructor that accepts IEnumerable<T> - so in C# (my VB isn't great):

List<Car> myNewCars = new List<Car>(CarServiceProxy.GetCars());

If you can change things; perhaps consider things like the WCF usage where you can specify the collection type (of all methods) - the scvutil /collectionType: switch. Changing to a WCF stack is non-trivial, though.

Marc Gravell
When using the /collectionType parameter mind the funky generic-notation with the blackquote mark. Eg. if you want to force svcutil to use List<T> collections then you have to specify this: /ct:System.Collections.Generic.List`1
Vizu