locked
cant pass list RRS feed

  • Question

  • I am using silverlight3 to use a webservice to update a DB.

    I cant pass a list of string to a service refernce service1.svc file.
    I have tried many variations but I dont understand the error

    ErrorValue of type 'System.Collections.Generic.List(Of String)' cannot be converted to 'System.Collections.ObjectModel.ObservableCollection(Of String)'.

     

    this recieves the 3rd paramter

        <OperationContract()> _
    Public Sub updatedataTest(ByVal myid As String, ByVal totalCorrect As Integer, ByVal mytestList As List(Of String))

    I pass this list to the above by

     Public Sub updateDataTest(ByVal type As Integer, ByVal mytestList As List(Of String))
            Dim webservice2 As New trigTrainer4.ServiceReference1.Service1Client
            Dim myresultsList As New List(Of String)

                  myresultsList = mytestList
                webservice2.updatedataTestAsync(myid, totalCorrect, myresultsList)
           

    I get the  list string by

     Public ReadOnly Property myResultsTestList() As List(Of String)
            Get
                Return mytestList
            End Get

        End Property

    Friday, July 23, 2010 10:00 AM

Answers

  • List<string> listStrings = new List<String>();
    ObservableCollection<string> ocStrings = new ObservableCollection<string>();
    
    listStrings.Add("Hello");
    listStrings.Add("World");
    
    // Following failed to convert
    //obString = (ObservableCollection<string>)listStrings;
    
    // This one works
    ocStrings = new ObservableCollection<string>(listStrings);
    
    // This one also works
    ocStrings = new ObservableCollection<string>();
    foreach (String s in listStrings)
        ocStrings.Add(s);


     

    Friday, July 23, 2010 12:45 PM

All replies

  • Change myresultsList to an ObservableCollection, then on this line

    myresultsList = mytestList

    cast  mytestList as an OC.

    Friday, July 23, 2010 10:22 AM
  • If the myTestList is a List, you cannot directly cast it to a ObservableCollection.  You will have to go through a for-loop process to copy the items over.

    ObservableCollection<string> myresultsList = new ObservableCollection<string>();
    
    foreach (string s in mytestlist)
    {
       myresultsList.Add(s);
    }


     

    Friday, July 23, 2010 11:14 AM
  • Right, in SL3, but in SL4 you can? 

    Friday, July 23, 2010 11:19 AM
  • List<string> listStrings = new List<String>();
    ObservableCollection<string> ocStrings = new ObservableCollection<string>();
    
    listStrings.Add("Hello");
    listStrings.Add("World");
    
    // Following failed to convert
    //obString = (ObservableCollection<string>)listStrings;
    
    // This one works
    ocStrings = new ObservableCollection<string>(listStrings);
    
    // This one also works
    ocStrings = new ObservableCollection<string>();
    foreach (String s in listStrings)
        ocStrings.Add(s);


     

    Friday, July 23, 2010 12:45 PM
  • That error occurs because your webService is configured with the ObservableCollection.. And you are passing List which is Generic.


    To resolve this problem, Configure your Service Reference by right clicking on Service Reference.

    Now change the Collection type to the Generic.List from ObservableCollection.


    Plz try this one..

    and reply if this one useful...

    Monday, August 2, 2010 8:46 AM
  • ok i did this a while back but WCF is not working when i deployed on the host.

    It has been awful to use as it works on my PC but not in the host.


    check my other posts on it.


    Monday, August 2, 2010 9:21 AM