2

I'm trying to print object element in my list with foreach loop in main scope, but the only thing that prints out is :

ConsoleApplication1.WorldMap

What have gone wrong here and how do I get it to print out the actual elements?

using ConsoleApplication1;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
     class WorldMap
     {

        private string higher;
        private string lower;

        public worldMap()
        {
        }

        public WorldMap(string higher, string lower)
        {
           this.higher = higher;
           this.lower = lower;
        } 

        public string Higher
        {
            get { return higher; }
            set { higher = value; }
        }

        public string Lower
        {
            get { return Lower; }
            set { Lower = value; }
        }
    }   
  class Program
  {

    static void Main(string[] args)
    {

        WorldMap map = new WorldMap();

        List<WorldMap> mapList = new List<WorldMap>();
        mapList.Add(new WorldMap(map.Lower, map.Higher));

        Console.WriteLine("\tInsert highest point:");
        map.Highest= Console.ReadLine();
        Console.WriteLine("\tInsert lowest point");
        map.Lowest= Console.ReadLine();


        using (StreamWriter writer = new StreamWriter(@"C:\freq.txt", true))
        { 
            writer.WriteLine(map.Lower + map.Higher); 
        }

        foreach (object view in mapList)
        {
            Console.WriteLine(view);
        }
      }
   }
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
Global
  • 103
  • 3
  • 13
  • Yes, thanks a lot for your answers! Nothing appears in the console but I'll check your answers once again. – Global Mar 12 '16 at 13:41
  • Also do you know why nothing appears in the console? Because in this line `mapList.Add(new WorldMap(map.Lower, map.Higher));` the `map.Lower` and `map.Higher` are empty strings you should send a value for constructor like this: `mapList.Add(new WorldMap("lower", "higher"));`. – Salah Akbari Mar 12 '16 at 13:49
  • It works now, thanks! Used override and called the object in main by writeline. Which one of them is preferable in common enviroments? One step further... – Global Mar 12 '16 at 13:58
  • I prefer the `override`. – Salah Akbari Mar 12 '16 at 13:59
  • So there's two options with 1 output, Console.WriteLine(map.ToString()); Is there any difference of using following? (map.Lower, map.Higher)); Or mapList.Add(new WorldMap("lower", "higher")); : mapList.Add(new WorldMap("lower", "higher"));. – Global Mar 12 '16 at 14:13
  • Have a look at this: https://msdn.microsoft.com/en-us/library/ms173154.aspx – Salah Akbari Mar 12 '16 at 14:18

2 Answers2

2

Because you didn't override the ToString method of WorldMap so Console.WriteLine(view) returns the name of the class which is WorldMap. You could either override the ToString method in the class or call the properties in the foreach loop but don't forget you should change object to var. Like this:

class WorldMap
{

   ....
   ....
   public override string ToString()
   {
      return Lower + " " + Higher;
   }
}

Or (change object to var):

foreach (var view in mapList)
{
     Console.WriteLine(view.Lower + " " + view.Higher);
}
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
0

view is of type WorldMap, Use it like this instead:

foreach (object view in mapList)
        {
            Console.WriteLine(view.Lower.ToString() + "," + view.Higher.ToString() );
        }
Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171