0

I am trying to Convert a List to an Array to add to my ArrayAdapter, but when I am trying to do that I get the error:

"Cannot implicitly convert type 'System.Collections.Generic.List[]' to 'string[]'"

This is my code:

List<ClassName> Names = new List<ClassName>();

foreach (var property in coinPropery)
{
    var propertyList = JsonConvert.DeserializeObject<List<Names>>(property.ToString());
    coins.AddRange(propertyList);
    Console.WriteLine(Names);
}

string[] NamesArray = Names.InArray();

The code in my ClassName is:

public class ClassName
{
    public string Name { get; set; }
}

What can I do?

  • Names is string, but list – Artem Tishchenko Jan 12 '18 at 15:41
  • I want to get an Array of all Names –  Jan 12 '18 at 15:46
  • To add an ArrayAdapter to add to a listview –  Jan 12 '18 at 15:46
  • 2
    `InArray` is not a method on `List` - unless youve written it - and in which case we need the code of that extension method. Also you use a class/struct `Names` but I dont see that anywhere in your question either. – Jamiec Jan 12 '18 at 15:52
  • Your `foreach` loop doesn't do anything interesting.. and your call to `JsonConvert.DeserializeObject` will most likely throw an error, since you can't use a variable like a class. I get renaming things for the public.. but you better make sure you haven't introduced errors due to that renaming. – Sam Axe Jan 12 '18 at 15:58
  • Is it intentional here that Names will never contain any entries? – Kyle Burns Jan 12 '18 at 16:41

1 Answers1

2

I'm assuming that you are trying to do ToArray() instead of InArray()

Your problem is you are assigning an array of ClassName to an array of string

You can try:

string[] NamesArray = Names.Select(c=>c.Name).ToArray();
Aman B
  • 2,276
  • 1
  • 18
  • 26