IDictionary<DateTime, Double> get(string ticker, string type, DateTime from, DateTime To);
IDictionary<DateTime, Double> get(string ticker, string type, DateTime from, DateTime To);
Can anyone explain what the above function does and what Idictionary is for?
Liutauras MedziunasPosted Feb 8, 2011, 4:49 PM
Dictionary is a collection with key and value. The function get(string ticker, string type, DateTime from, DateTime To) just gets a dictionary with double values mapped with date time; IDictionary it is an interface. You could use it if you want to create dictionary by yourself with specific logic in it. What you should to do is just inherit IDictionary and do like in my sample. In this sample you see the implementation and you can use it when you exactly know your key and value object types. The Entry class is like a mapper to map your key with assigned value. Don't use this sample in real it just for your understanding. And you should read the article in msdn: http://msdn.microsoft.com/en-us/library/s4ys34ea.aspx from Suthish Nair for better understanding.
public class SmallDictionary : IDictionary<DateTime, Double>
{
private class Entry
{
public DateTime Key { set; get; }
public Double Value { set; get; }
public Entry(DateTime key, Double value)
{
this.Key = key;
this.Value = value;
}
}
private List<Entry> entries = new List<Entry>();
public void Add(DateTime key, double value)
{
entries.Add(new Entry(key, value));
}
public double this[DateTime key]
{
get
{
double result = 0;
Entry entry = FindEntry(key);
if (entry != null)
{
result = entry.Value;
}
else
{
throw new IndexOutOfRangeException("Key not found!");
}
return result;
}
set
{
Entry entry = FindEntry(key);
entry.Value = value;
}
}
private Entry FindEntry(DateTime key)
{
Entry resultEntry = null;
foreach (var entiry in entries)
{
if (entiry.Key == key)
{
resultEntry = entiry;
break;
}
}
return resultEntry;
}
// additional implementation goes here
....................
}
public static void Main()
{
SmallDictionary myDictionary = new SmallDictionary();
DateTime firstValueChangeDateTime = DateTime.Today;
DateTime secondValueChangeDateTime = firstValueChangeDateTime.AddHours(1);
// You can add items to to dictionary
myDictionary.Add(firstValueChangeDateTime, 50);
myDictionary.Add(secondValueChangeDateTime, 60);
// You can get items from dictionary
Console.WriteLine("Second: {0}", myDictionary[secondValueChangeDateTime]);
Console.WriteLine("First: {0}", myDictionary[firstValueChangeDateTime]);
// You can cange value by key
myDictionary[secondValueChangeDateTime] = 500;
myDictionary[firstValueChangeDateTime] = 800;
// You can get items from dictionary
Console.WriteLine("Second: {0}", myDictionary[secondValueChangeDateTime]);
Console.WriteLine("First: {0}", myDictionary[firstValueChangeDateTime]);
try
{
Console.WriteLine("First: {0}", myDictionary[DateTime.Now]);
}
catch(Exception exc)
{
Console.WriteLine(exc);
}
Console.ReadKey();
}
Liutauras MedziunasPosted Feb 9, 2011, 2:36 PM
Tommy SinPosted Feb 9, 2011, 1:58 PM
IDictionary<DateTime, Double> get(string ticker, string type, DateTime from, DateTime To);
In this case, is "IDictionary<DateTime, Double>" return type of the method?
Thanks
Liutauras MedziunasPosted Feb 9, 2011, 1:44 PM
Probably IDictionary
Tommy SinPosted Feb 9, 2011, 10:17 AM
However, what is the point of inheriting "IDictionary
public class SmallDictionary: IDictionary<DateTime, Double>
It seems that it works fine without the inheritance of the IDictionary interface.
Likewise, what is "get(string ticker, string type, DataTime from, DateTime To);" for in the following sentence and how it works? (The example you provided is not like the following format)
IDictionary<DateTime, Double> get(string ticker, string type, DateTime from, DateTime To);
Thanks
Suthish NairPosted Feb 8, 2011, 3:19 PM