Sunday, August 09, 2009

Convert Array to Generic List

You can happily use .NET Generics in WCF Services. But when you will get the proxy of the respective service, these generics will get converted into Array, even you are using .NET to develop the client application. So now the question is how I should get back from Array to Generic List again.

VB.NET

Simple Example

  Dim someArray() As Integer = {1, 2, 3, 4, 5}

        Dim genericList As New List(Of Integer)

        genericList.AddRange(someArray)

 

Web Service Example

  Dim resp As New MyLookupService.GetCountriesResponse

        Dim reqs As New MyLookupService.GetCountriesRequest

        Dim listData As New List(Of MyLookupService.CountryData)

        Dim arrData As Array

 

        reqs.LoginInfo = loginReq

        resp = srvObj.GetCountries(reqs)

        arrData = resp.CountriesInfo

        listData.AddRange(arrData)

 

        Me.gvwCountry.DataSource = listData

        Me.gvwCountry.DataBind()

 

Alternate Option

        When you Add Service Reference, click on "Advanced..." button on bottom left and Choose Collection Type from Dropdown as "System.Collections.Generic.List" and you are all set. No need to use array.

 

No comments: