Jump to content

c# print out list.

vorticalbox
Go to solution Solved by madknight3,

need to print out the whole list, so far i have.

 

-snip-

 

which does work, worked it out after i posted -.- there a better way to do this?

 

Since you're not using 'i', you could use a foreach loop. You can also get rid of the variable and just do everything in Console.WriteLine().

foreach (var list in playlist){    Console.WriteLine("{0}\t\t{1}\t\t{2}", list.ID, list.Name, list.Rate);}

David's suggestion of configuring the ToString() method in the Playlist class is also a good one if the formatting is the same all the time. That would change it to look more like

foreach (var list in playlist){    Console.WriteLine(list.ToString());}

gonna have to explain that tosting a bit more? 

 

this is for uni and the course says i have to use XML  :(

 

To set up ToString(), you just need to override it in the class.

class Playlist{    public int ID;    public string Name;    public int Rating;    public override string ToString()    {        return string.Format("{0}\t\t{1}\t\t{2}", ID, Name, Rating);    }}

David also mentioned having another class that holds the list of playlists with it's own ToString() method that constructs and prints everything for you. Something like this

class AllPlaylists{    public List<Playlist> playlist    // A better way might be to make the above private and do everything, like Add/Remove through this class    // so you're not directly interacting with the list.    public override string ToString()    {        var sb = new StringBuilder();        sb.AppendLine("ID\t\tName\t\tRating");                foreach (var list in playlist)        {            sb.AppendLine(list.ToString());        }        return sb.ToString();    }}

The above class may be a little overkill if all you're doing is printing out the list. However if you need to do other operations on the list of playlists, it can be useful to have that extra level of abstraction.

 

 

David can correct me if I interpreted his response incorrectly.

I'm back again with new problems :) currently pulling xml from a web service and storing it in a list (seems a good way but feel free to correct me)

 

Class 

class Playlist        {            public int ID;            public string Name;             public int Rating;        }

And the listy bit

 string xdoc = client.TopTen();            XmlDocument doc = new XmlDocument();            doc.LoadXml(xdoc);            List<Playlist> playlist = new List<Playlist>();            XmlNodeList nodes = doc.DocumentElement.SelectNodes("/Lists/Playlist");            foreach(XmlNode node in nodes)            {                Playlist play = new Playlist();                play.ID = Convert.ToInt32(node.Attributes["ID"].Value);                play.Name = node.SelectSingleNode("PlaylistName").InnerText;                play.Rate = Convert.ToInt32(node.SelectSingleNode("Rating").InnerText);                playlist.Add(play);            }

XML

<Lists>  <Playlist ID='6'>    <PlaylistName>Test</PlaylistName>    <Rating>5</Rating>  </Playlist>  <Playlist ID='10'>    <PlaylistName>fire</PlaylistName>    <Rating>3</Rating>  </Playlist></Lists> 

Back ground the web service lets you create a playlist, this function gets the top ten rated playlists, list is created and sorted server side so its just garbing the XML and printing them out. This is a console application so i can test it all works before moving it to a GUI.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

I'm back again with new problems  :) currently pulling xml from a web service and storing it in a list (seems a good way but feel free to correct me)

 

-snip-

 

Back ground the web service lets you create a playlist, this function gets the top ten rated playlists, list is created and sorted server side so its just garbing the XML and printing them out. This is a console application so i can test it all works before moving it to a GUI.

 

So what's the problem?

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

 

 

 

So what's the problem?

 

need to print out the whole list, so far i have.

            Console.WriteLine("ID\t\tName\t\tRating");            for (int i = 0; i < playlist.Count; i++)            {                string A = "";                A += string.Format("{0}", playlist[i].ID);                A += string.Format("\t\t{0}", playlist[i].Name);                A += string.Format("\t\t{0}", playlist[i].Rate);                Console.WriteLine(A);            } 

which does work, worked it out after i posted -.- there a better way to do this?

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

need to print out the whole list, so far i have.

            Console.WriteLine("ID\t\tName\t\tRating");            for (int i = 0; i < playlist.Count; i++)            {                string A = "";                A += string.Format("{0}", playlist[i].ID);                A += string.Format("\t\t{0}", playlist[i].Name);                A += string.Format("\t\t{0}", playlist[i].Rate);                Console.WriteLine(A);            } 

which does work, worked it out after i posted -.- there a better way to do this?

 

A more elegant way of doing this is to build up objects to represent a single Playlist and a list of playlists and define their toString methods. The toString of playlist would build the string A in your for loop above and the toString of the list of Playlists would just print out every playlist.toString in its list. Essentially the class representing the list of playlists is a wrapper around your list that lets you add more functionality onto the list. You can add methods to this class that would get you all 5 star rated playlists for example.

 

Also if you are able to get JSON back from the webservice I'd suggest trying that and using a library such as Newtonsoft's JSON parser which can take care of most of the actual assigning of values from the JSON by using attributes on the object members of the Playlist class.

Link to comment
Share on other sites

Link to post
Share on other sites

A more elegant way of doing this is to build up objects to represent a single Playlist and a list of playlists and define their toString methods. The toString of playlist would build the string A in your for loop above and the toString of the list of Playlists would just print out every playlist.toString in its list. Essentially the class representing the list of playlists is a wrapper around your list that lets you add more functionality onto the list. You can add methods to this class that would get you all 5 star rated playlists for example.

 

Also if you are able to get JSON back from the webservice I'd suggest trying that and using a library such as Newtonsoft's JSON parser which can take care of most of the actual assigning of values from the JSON by using attributes on the object members of the Playlist class.

gonna have to explain that tosting a bit more? 

 

this is for uni and the course says i have to use XML :(

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

need to print out the whole list, so far i have.

 

-snip-

 

which does work, worked it out after i posted -.- there a better way to do this?

 

Since you're not using 'i', you could use a foreach loop. You can also get rid of the variable and just do everything in Console.WriteLine().

foreach (var list in playlist){    Console.WriteLine("{0}\t\t{1}\t\t{2}", list.ID, list.Name, list.Rate);}

David's suggestion of configuring the ToString() method in the Playlist class is also a good one if the formatting is the same all the time. That would change it to look more like

foreach (var list in playlist){    Console.WriteLine(list.ToString());}

gonna have to explain that tosting a bit more? 

 

this is for uni and the course says i have to use XML  :(

 

To set up ToString(), you just need to override it in the class.

class Playlist{    public int ID;    public string Name;    public int Rating;    public override string ToString()    {        return string.Format("{0}\t\t{1}\t\t{2}", ID, Name, Rating);    }}

David also mentioned having another class that holds the list of playlists with it's own ToString() method that constructs and prints everything for you. Something like this

class AllPlaylists{    public List<Playlist> playlist    // A better way might be to make the above private and do everything, like Add/Remove through this class    // so you're not directly interacting with the list.    public override string ToString()    {        var sb = new StringBuilder();        sb.AppendLine("ID\t\tName\t\tRating");                foreach (var list in playlist)        {            sb.AppendLine(list.ToString());        }        return sb.ToString();    }}

The above class may be a little overkill if all you're doing is printing out the list. However if you need to do other operations on the list of playlists, it can be useful to have that extra level of abstraction.

 

 

David can correct me if I interpreted his response incorrectly.

Link to comment
Share on other sites

Link to post
Share on other sites

I also concure that ToString overriding is the way to go as it encapsulates all of that code in each of the respective classes. In other words it helps to promote a clean design.

 

As @DavidTheWin mentioned Json.NET is good for what you are trying to do as it can serialize/deserialize to both JSON and XML.

 

The only thing that I would add at this point is that if I had more than one serialization concern I'd start thinking about putting the implementation details behind an interface - possibly implementing it in terms of a strategy pattern with maybe a factory.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

ty guys :P this is actually just for testing all the functions on my web service actually work. the client will actually be a GUI based.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×