Based on an abstract class the programs adds values to a collection. Problem 1: When displaying the added values they all are over written with the latest added value. As a side problem, adding the values seems to tedious, there must be better way to achieve this.
Browsing through other answers, there are similar issues using a static class, however that is not the case here. I tried with removing the "abstract" which makes no difference in output.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QuoteCoScMe
{
class Program
{
public abstract class Koers
{
public string fonds { get; set; }
public DateTime datum { get; set; }
public Double koers { get; set; }
}
public class Historical : Koers
{
}
private static void Display(Collection<Historical> cs)
{
Console.WriteLine();
foreach (Historical item in cs)
{
Console.WriteLine("{0} {1} {2} ", item.fonds, item.datum.ToString(), item.koers);
}
}
static void Main(string[] args)
{
Historical xkoers = new Historical() ;
Collection<Historical> Historicals = new Collection<Historical>();
xkoers.fonds = "AL1";
xkoers.datum = DateTime.Parse("2018-05-08");
xkoers.koers = 310.1;
Historicals.Add(xkoers);
xkoers.fonds = "AL2";
xkoers.datum = DateTime.Parse("2018-06-08");
xkoers.koers = 320.1;
Historicals.Add(xkoers);
xkoers.fonds = "Some other 3";
xkoers.datum = DateTime.Parse("2019-06-08");
xkoers.koers = 20.1;
Historicals.Add(xkoers);
Display(Historicals);
/* Question 2: this is a tedious way of adding, i would want to use xkoers.add("AL2", DateTime.Parse("2018-05-08"), 320); */
/* Question 1: when displaying the historicals for some reason the whole list contains only the latest added item in the list.
In de VS debugger is shows that all list items have the same values.
Output:
Some other 3 8/06/2019 0:00:00 20,1
Some other 3 8/06/2019 0:00:00 20,1
Some other 3 8/06/2019 0:00:00 20,1
Press any key to continue . . .
*/
}
}
}