0

Hello I've been searching around for the best way to crate a "2 dimensional" List. And came across an example given in: Are 2 dimensional Lists possible in c#?

But I'm not getting the sorta output I were hoping for. Running below code my goal is to proper Console.Writeline( Stuff in the track list ), but all the console is writing is HelloWorld.Track

namespace HelloWorld
    {
        class Track
        {
            public int TrackID { get; set; }
            public string Name { get; set; }
            public string Artist { get; set; }
            public string Album { get; set; }
            public int PlayCount { get; set; }
            public int SkipCount { get; set; }

          //  List<Track> trackList = new List<Track>();

            public void add()
            {
                var trackList = new List<Track>();
                trackList.Add(new Track
                {
                    TrackID = 1234,
                    Name = "I'm Gonna Be (500 Miles)",
                    Artist = "The Proclaimers",
                    Album = "Finest",
                    PlayCount = 10,
                    SkipCount = 1

                });

                Track firstTrack = trackList[0];
                Console.WriteLine(firstTrack);
                Console.ReadKey();
            }
        }
    }

Also what would be the termology name for such a case? Searching for c# track on google just suggest guitar videos.

Community
  • 1
  • 1

2 Answers2

2

The problem with the Console.WriteLine is that you are passing in the object instance. What it is doing is calling the ToString method on your object. Since you have not overridden that method in your Track class, it executes the default implementation which is to output the type name of the object. You can override the ToString method to return your own custom string. See https://msdn.microsoft.com/en-us/library/system.object.tostring(v=vs.110).aspx on how to do that. Or you can change how you're calling the Console.WriteLine method to pass in the specific message and object property values that you want to have displayed instead of just passing the object instance.

Matt Thalman
  • 3,540
  • 17
  • 26
1

my goal is to proper Console.Writeline( Stuff in the track list ), but all the console is writing is HelloWorld.Track.

That is because you are printing the object and not it's properties. Which can be done using following code:

Console.WriteLine(String.Format("{0}:{1}:{2}:{3}:{4}:{5}", firstTrack.TrackID, firstTrack.Name, firstTrack.Artist, firstTrack.Album, firstTrack.PlayCount, firstTrack.SkipCount));

For extra help, printing all properties from a certain object, try this link: ObjectDumper Stackoverflow

For more information about how Console.WriteLine() works: Console.WriteLine (MSDN)

Community
  • 1
  • 1
Max
  • 12,622
  • 16
  • 73
  • 101