0

Is it possible to create an event before an item is added or updated to an IDictionary?

public static IDictionary<string, Aircraft> aircraftList = new Dictionary<string, Aircraft>();

and I use it like this

MainForm.aircraftList[acNumber].FlightStatus = strFlying;

What I want to do is not to update the value if the new value is null, empty string or an int == 0

Aircraft is a class with lots of values which are a mix of strings int's and doubles...

or is there a way of doing this type of action within the class?

currently I'm doing this everywhere I assign a value via an if statement, I assume there is a better way?

Adrian
  • 1,089
  • 24
  • 51

2 Answers2

2

You could roll out your own Dictionary class and use custom Add() function (and other functions). Something like this:

class MyCustomDictionary : Dictionary<string, Aircraft>
{
  public new Add(string key, Aircraft value)
  {
    if (YOUR_CONDITION)
    {
      base.Add(key, value);
    }
  }
}

You should note the use of new keyword here. The Add() function cannot be overridden because the base class doesn't define it as overridable. So you must use new in your definition.

dotNET
  • 33,414
  • 24
  • 162
  • 251
  • thank you I instated it thus: public static MyCustomDictionary aircraftList = new MyCustomDictionary(); . However it never seems to raise the event. i have set a break point on the if condition. – Adrian Apr 01 '17 at 11:45
  • @Adrian: It won't raise any events. It will just allow you to place your condition in one central place, which will be checked before actually adding the item to the dictionary. – dotNET Apr 01 '17 at 13:15
  • maybe I'm misunderstanding, sorry if so, but the condition never hits the breakpoint I put a console.WriteLine before it too, but it is never executed! – Adrian Apr 01 '17 at 13:29
  • @Adrian: Must be something in your implementation. Do you see multiple overloads of the `Add()` function from where u call it? – dotNET Apr 01 '17 at 13:34
  • Consider using an ObservableCollection – Heinz Kessler Apr 01 '17 at 14:52
  • @HeinzKessler: What for? – dotNET Apr 01 '17 at 15:02
0

I have to be honest with you, I'm a bit confused as to what you are requesting... However, I've noticed a couple of issues with your example code which could be causing the problem you're experiencing?

First off, IDictionary is a collection object interface, and yet you are treating it as an array, which to me is a tad suspicious.

Secondly, Aircraft is a class and yet you are not initialising it when setting a value to FlightStatus.

So what you should be doing is either:

aircraftList.Add(strKey, new Aircraft() {FlightStatus = strFlying});

or

Aircraft ac = new Aircraft();

// set the ac values
ac.FlightStatus = strFlying;

aircraftList.Add(strKey, ac);

You can add conditional statement before adding the object to the dictionary.

Finally, what do you mean when asking about adding a before event? If you follow dotNET's example you could add your own OnBeforeEvent method/Invoke() within Add() method declaration.

Overdrive77
  • 115
  • 2
  • 9