3

I have an error "Cannot implicitly convert type "CorrespondingBall.MyCloudService.ArrayOfString' to 'System.Collections.Generic.List". Does anyone know how can I solved it? I have read up on Convert array of strings to List<string>, but I do not understand and implement it. My codes are below.

ServiceSoapClient client = new ServiceSoapClient();

List<string> objectiveList = new List<string>();

client.getObjectiveCompleted += new EventHandler<getObjectiveCompletedEventArgs>(getObjectiveCompletedHandler);
client.getObjectiveAsync();

I received the error for

private void getObjectiveCompletedHandler(object sender, getObjectiveCompletedEventArgs e)
    {
        objectiveList = e.Result;
    }
Community
  • 1
  • 1
Liu Jia Hui
  • 231
  • 2
  • 8
  • 16
  • I don't know what a `CorrespondingBall.MyCloudService.ArrayOfString` is, but can you use a foreach and add them to the list one by one? – Jonesopolis Aug 22 '13 at 01:29
  • Hi @Jonesy, I am using a web service, MyCloudService and the application name is CorrespondingBall. – Liu Jia Hui Aug 22 '13 at 01:34
  • Are you return `List` from your service method? or `string[]`? what is your web service (WCF, ASMX..)? are you adding service reference or web reference? – Damith Aug 22 '13 at 02:49

2 Answers2

4

If ArrayOfString is truly a basic array of string types, you can do the following:

    string[] names = { "John", "Doe" };

    List<string> namesList = new List<string>(names);

    // OR

    List<string> namesList2 = new List<string>();

    foreach (string name in names)
    {
        namesList2.Add(name);
    }
Inisheer
  • 20,376
  • 9
  • 50
  • 82
  • Hi @Inisheer, what if the values are from database? – Liu Jia Hui Aug 22 '13 at 01:37
  • @LiuJiaHui It doesn't matter the source of the data. More important is how you are retrieving and storing that data in your code. You will need to provide additional code in order to provide a more detailed solution. – Inisheer Aug 22 '13 at 01:39
2

Supposing e.Result is an array of string you can do it like this

private void getObjectiveCompletedHandler(object sender, getObjectiveCompletedEventArgs e)
    {
      List<string> namesList = new List<string>();

      foreach (string name in e.Result)
       {
        namesList.Add(name);
       }
    }
TheProvost
  • 1,832
  • 2
  • 16
  • 41
  • This is ofchurs depending if e.Result is really returning array. If not then you should change your question to how to return an array from your cloud service – TheProvost Aug 22 '13 at 02:58
  • Hi @TheProvost, it is returning a list. – Liu Jia Hui Aug 22 '13 at 03:01
  • If its returning a list as you say it does. then objectiveList = e.Result; would not have triggered an error. Im sure the problem is with sending the correct object type rather than conversion – TheProvost Aug 22 '13 at 03:04