I have a SortedDictionary
of events which people can purchase tickets for. The event code is unique to that ticket which is stored as the key.
protected SortedDictionary<int, Event> Events = new SortedDictionary<int, Event>();
An example of this would look like so:
Events.Add(123, new Event(...));
I implemented a logger to be able to track the Event. Which looks like so:
class EventLogger
{
protected SortedDictionary<int, EventLogType[]> Events = new SortedDictionary<int, EventLogType[]>();
public EventLogger()
{
}
public void Log(int eventCode, EventLogType type)
{
KeyValuePair<int, EventLogType[]> e;
if (Events.ContainsKey(eventCode))
{
(e = Events.FirstOrDefault(x => x.Key.Equals(eventCode)))
.Value[e.Value.Count()] = type;
}
else Events.Add(eventCode, new EventLogType[] { type });
}
}
enum EventLogType
{
PURCHASE,
CREATE,
UPDATE,
DELETE
}
When, for example, I create an event, I have to now call this Log method:
(new EventLogger()).Log(123, EventLogType.CREATE); // EventLogger instance is stored static
Which is becoming a serious pain whenever I want to update, remove, or create. I looked on SO about changing how the set
works for the Array and tried this:
protected SortedDictionary<int, Event> Events { get; set {
this.save();
} } = new SortedDictionary<int, Event>();
But this throws me a IDE error:
Only auto-implemented properties can have initializers.
How can I access the set
call before anything is stored, or updated in the array. How can I check which action is being done? So rather than make a call to the Log separately, it can be done in the .Add
or when its Value is updated or deleted?
My current code looks like this:
Events.Add(123, new Event(...));
Logger.Log(123, EventLogType.CREATE);
Where as I want it to log the create when I just do a call to .Add