Locations test = new Locations();
Location loc = new Location();
test.Add(sSite,
sBldg)
test.Add(sSite1)
test.Add(sSite2)
string printitout = string.Join(",", test); //having issues outputting whats on the list
It seems like you would be able to write an extension method to accomplish this: (Syntax may be a little off - I don't currently have access to a resource)
public void IList<T> WriteListToConsole(this IEnumerable<T> source)
{
var list = source as IList<T>;
foreach(string item in list)
{
Console.WriteLine(item);
}
}
nhat11
Member
10 Points
15 Posts
Trying to string.Join an IList and outputting the results to console.
Apr 12, 2012 02:56 PM|LINK
I'm trying to implement the first example http://www.dotnetperls.com/convert-list-string into my method
Using "string.Join(",", test);" works (since I'm using 4.0), for some reason my output, outputs:
"Ilistprac.Location, Ilistprac.Location, Ilistprac.Location"
for some reason instead of what's in the list.
Also is there a way to implement Foreach also from this example? http://stackoverflow.com/questions/759133/how-to-display-list-items-on-console-window-in-c-sharp
List<string> strings = new List<string> { "a", "b", "c" };
strings.ForEach(Console.WriteLine);
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 sBldg = "test32";
string sSite1 = "test";
string sSite2 = "test";
Locations test = new Locations();
Location loc = new Location();
test.Add(sSite, sBldg)
test.Add(sSite1)
test.Add(sSite2)
string printitout = string.Join(",", test); //having issues outputting whats on the list
}
}
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;
loc.Bldg = sBldg;
_locs.Add(loc);
}
private string _bldg = string.Empty;
public string Bldg
{
get { return _bldg; }
set { _bldg = value; }
}
}
Rion William...
All-Star
31882 Points
5191 Posts
Re: Trying to string.Join an IList and outputting the results to console.
Apr 12, 2012 03:24 PM|LINK
It seems like you would be able to write an extension method to accomplish this: (Syntax may be a little off - I don't currently have access to a resource)
public void IList<T> WriteListToConsole(this IEnumerable<T> source) { var list = source as IList<T>; foreach(string item in list) { Console.WriteLine(item); } }nhat11
Member
10 Points
15 Posts
Re: Trying to string.Join an IList and outputting the results to console.
Apr 12, 2012 04:44 PM|LINK
Yup, I can write an extension method for it and I have the IEnmerator implmented:
IEnumerator<Location> IEnumerable<Location>.GetEnumerator() //nothing
{
return _locs.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() //nothing
{
return _locs.GetEnumerator();
}
In "string printitout = string.Join(",", test);" the list is in there, I just can't figure out how to make it output as a string value.
Paul Linton
Star
13571 Points
2571 Posts
Re: Trying to string.Join an IList and outputting the results to console.
Apr 13, 2012 12:09 AM|LINK
You need to provide an override of ToString() in Location. Something as simple as
public override string ToString() { return Site;}
should be sufficient.
nhat11
Member
10 Points
15 Posts
Re: Trying to string.Join an IList and outputting the results to console.
Apr 13, 2012 04:56 PM|LINK
Yup that's the answer I have been looking for. Thanks!
Rion William...
All-Star
31882 Points
5191 Posts
Re: Trying to string.Join an IList and outputting the results to console.
Apr 13, 2012 04:59 PM|LINK
Glad it worked for you nhat11!