4

I'm trying to implement the first example http://www.dotnetperls.com/convert-list-string into my method but I'm having a hard time matching the 2nd argument for the method:

string printitout = string.Join(",", test.ToArray<Location>);

error message:

The best overloaded method match for 'string.Join(string,
System.Collections.Generic.IEnumerable<string>)' has some invalid arguments

All the IList interfaces are implemented with the IEnurmerable too (just not listed here unless someone wants me to).

class IList2
{
    static void Main(string[] args)
    {

     string sSite = "test";
     string sSite1 = "test";
     string sSite2 = "test";

     Locations test = new Locations();
     Location loc = new Location();
     test.Add(sSite)
     test.Add(sSite1)
     test.Add(sSite2)
     string printitout = string.Join(",", test.ToArray<Location>); //having issues calling what it needs.

     }
 }
string printitout = string.Join(",", test.ToArray<Location>);


public class Location
{
    public Location()
    {

    }
    private string _site = string.Empty;
    public string Site
    {
        get { return _site; }
        set { _site = value; }
    }
}

public class Locations : IList<Location>
{
    List<Location> _locs = new List<Location>();

    public Locations() { }

    public void Add(string sSite)
    {
        Location loc = new Location();
        loc.Site = sSite;
        _locs.Add(loc);
    }
 }

Edit: Ok using "string.Join(",", test);" works, before I close this with a check mark, for some reason my output, outputs:

"Ilistprac.Location, Ilistprac.Location, Ilistprac.Location"

for some reason instead of what's in the list.

nhat
  • 1,159
  • 5
  • 21
  • 40

4 Answers4

6

You don't need ToArray() at all (since it appears you're using .Net 4.0) so you can make the call

string.Join(",", test);
dlev
  • 48,024
  • 5
  • 125
  • 132
  • Just before I answered this good. My output outputs as: "Ilistprac.Location, Ilistprac.Location, Ilistprac.Location" and not the list items in the list for some reason. – nhat Apr 11 '12 at 19:32
  • You haven't overriden `ToString()` on your `Location` objects, so you are getting the default behavior. – dlev Apr 11 '12 at 19:41
  • @dlev - What does that mean haven't overridden ToString() on objects? The answer does not say anything about overriding ToString(). How do you fix that? – Rani Radcliff Oct 21 '19 at 15:05
2

You need to put parentheses - () - after ToArray<Location>:

string printitout = string.Join(",", test.Select(location => location.Site).ToArray()); 
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
  • ah lambda, I'm not good with that but I'm trying to learn it! That actually outputs whats in the IList correctly too. – nhat Apr 11 '12 at 19:35
2

If your Locaions type implements IEnumerable you won't need ToArray:

string printiout = String.Join(",", test);
the_joric
  • 11,986
  • 6
  • 36
  • 57
1

Try:

string printitout = string.Join(",", test);
code4life
  • 15,655
  • 7
  • 50
  • 82
  • 3
    Thanks for the reply, I get this Error Message: The call is ambiguous between the following methods or properties: 'string.Join(string, System.Collections.Generic.IEnumerable)' and 'string.Join(string, params object[])' – nhat Apr 11 '12 at 19:21