static void Main(string[] args) { HistoricValue newValue = new HistoricValue(); List <HistoricValue> Historicals = new List <HistoricValue>(); newValue.Name = "Some name 1"; newValue.Lastdate = DateTime.Parse("2018-05-08"); newValue.Value = 310.1; Historicals.Add(new HistoricValue() { Name = newValue.Name, Lastdate = newValue.Lastdate, Value = newValue.Value }); Historicals.Add(newValue); Console.WriteLine("Expected output: Twice Some Name 1"); Display(Historicals); newValue.Name = "Some name 2"; newValue.Lastdate = DateTime.Parse("2018-09-09"); newValue.Value = 210.1; Historicals.Add(new HistoricValue() { Name = newValue.Name, Lastdate = newValue.Lastdate, Value = newValue.Value }); Historicals.Add(newValue); Console.WriteLine("\nExpected output: Twice Some Name 1 and twice somename 2"); Display(Historicals); Console.WriteLine("\nReceived output: once Some name 1 and tree times somename 2"); Console.WriteLine("\nnewValue get assigned values, what is stored in the list is the pointer to values, so item 2,3,4 will point to the same values in memory."); List <HistoricValue> Historicals2 = new List <HistoricValue>(); Console.WriteLine("\nRCorrect ways to fill the list can be by using a constructor"); Historicals2.Add(new HistoricValue() { Name = "Some name 1", Lastdate = DateTime.Parse("2018-05-08"), Value = 310.1 }); Historicals2.Add(new HistoricValue() { Name = "Some name 2", Lastdate = DateTime.Parse("2018-06-08"), Value = 100.1 }); Console.WriteLine("Expected output: Some Name 1 and Somename 2"); Display(Historicals2); Console.WriteLine("\nOr add with specifically creating a new posistion in the list and add it."); newValue.Name = "Some name 3"; newValue.Lastdate = DateTime.Parse("2018-05-08"); newValue.Value = 310.1; Historicals2.Add(new HistoricValue() { Name = newValue.Name, Lastdate = newValue.Lastdate, Value = newValue.Value }); newValue.Name = "Some name 4"; newValue.Lastdate = DateTime.Parse("2018-09-09"); newValue.Value = 999; Historicals2.Add(new HistoricValue() { Name = newValue.Name, Lastdate = newValue.Lastdate, Value = newValue.Value }); Console.WriteLine("Expected output: Some Name 1,2,3 and 4"); Display(Historicals2); Console.WriteLine("\nOr through using a loop in wich a variable is created and assiged and then stops living."); for (int x = 5; x <= 7; x++) { HistoricValue newValueInLoop = new HistoricValue(); newValueInLoop.Name = "Some name " + x.ToString(); newValueInLoop.Lastdate = DateTime.Parse("2018-09-09"); newValueInLoop.Value = 999 + x; Historicals2.Add(new HistoricValue() { Name = newValueInLoop.Name, Lastdate = newValueInLoop.Lastdate, Value = newValueInLoop.Value }); //Display(Historicals2); } Console.WriteLine("Expected output: Some Name 1,2,3,4,5,6,7"); Display(Historicals2); Console.WriteLine("Actually this is strange, realizing the variable only exists in the loop, yet the memory values are retainted, i hope the garbage collector works"); }
public static InflationCurveSnapshot GetInflationCurveSnapshot(int InflationCurveID, DateTime d) { InflationCurveSnapshot ics = new InflationCurveSnapshot(); ics.Id = InflationCurveID; ics.settlementDate = d; ics.Name = CachedInflationCurveDataDic[InflationCurveID].Name; ics.Family = CachedInflationCurveDataDic[InflationCurveID].Family; ics.CurrencyId = CachedInflationCurveDataDic[InflationCurveID].CurrencyId; ics.InflationIndexId = CachedInflationCurveDataDic[InflationCurveID].InflationIndexId; ics.InflationIndex = CachedInflationIndexDic[ics.InflationIndexId]; ics.settings = CachedInflationCurveDataDic[InflationCurveID].settings; List <EntryValuePair> ListEVP = new List <EntryValuePair>(); foreach (InflationCurveEntryData iced in CachedInflationCurveEntryDataDic.Values.ToList()) { EntryValuePair evp = new EntryValuePair(); //to be added to ListEVP if ((iced.InflationCurveId == InflationCurveID) && (iced.ValidDateBegin <= d) && (iced.ValidDateEnd >= d) ) { // ics.EntryList.Add(iced); //now for that entry we need to find the most recent historical value evp.iced = iced; if (iced.Type == "inflationbond") { List <RateHistory> bh = getBondsHistoryById(iced.Instrument.Id, d); RateHistory rh = bh[bh.Count() - 1]; if (rh.RateId != iced.Instrument.Id) { MessageBox.Show("something is wrong in GetInflationCurveSnapshot"); } else { evp.value = bh[bh.Count() - 1].Close; // ics.ValueList.Add(bh[bh.Count() - 1].Close); } } else if (iced.Type == "swap") { List <RateHistory> bh = getRateHistoryById(iced.Instrument.Id, d); RateHistory rh = bh[bh.Count() - 1]; if (rh.RateId != iced.Instrument.Id) { MessageBox.Show("something is wrong in GetInflationCurveSnapshot"); } else { // ics.ValueList.Add(bh[bh.Count() - 1].Close); evp.value = bh[bh.Count() - 1].Close; } } ListEVP.Add(evp); } } // ics.EntryList.Sort((x, y) => y.Date.CompareTo(x.Date)); ListEVP.Sort(); ics.EntryList = new ObservableCollection <InflationCurveEntryData>(); ics.ValueList = new ObservableCollection <double>(); foreach (EntryValuePair evp in ListEVP) { ics.EntryList.Add(evp.iced); ics.ValueList.Add(evp.value); } List <RateHistory> iihistory = getIndexHistoryById(ics.InflationIndexId, d); ics.IndexHistory = new ObservableCollection <HistoricValue>(); foreach (RateHistory iirh in iihistory) { HistoricValue vp = new HistoricValue(); vp.Date = iirh.Date; vp.Value = iirh.Close; ics.IndexHistory.Add(vp); } return(ics); }