Unit testing that events are raised in C# (in order) - c#

I have some code that raises PropertyChanged events and I would like to be able to unit test that the events are being raised correctly.
The code that is raising the events is like
public class MyClass : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public string MyProperty
{
set
{
if (_myProperty != value)
{
_myProperty = value;
NotifyPropertyChanged("MyProperty");
}
}
}
}
I get a nice green test from the following code in my unit tests, that uses delegates:
[TestMethod]
public void Test_ThatMyEventIsRaised()
{
string actual = null;
MyClass myClass = new MyClass();
myClass.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
{
actual = e.PropertyName;
};
myClass.MyProperty = "testing";
Assert.IsNotNull(actual);
Assert.AreEqual("MyProperty", actual);
}
However, if I then try and chain the setting of properties together like so:
public string MyProperty
{
set
{
if (_myProperty != value)
{
_myProperty = value;
NotifyPropertyChanged("MyProperty");
MyOtherProperty = "SomeValue";
}
}
}
public string MyOtherProperty
{
set
{
if (_myOtherProperty != value)
{
_myOtherProperty = value;
NotifyPropertyChanged("MyOtherProperty");
}
}
}
My test for the event fails - the event that it captures is the event for the MyOtherProperty.
I'm pretty sure the event fires, my UI reacts like it does, but my delegate only captures the last event to fire.
So I'm wondering:
1. Is my method of testing events correct?
2. Is my method of raising chained events correct?

Everything you've done is correct, providing you want your test to ask "What is the last event that was raised?"
Your code is firing these two events, in this order
Property Changed (... "My Property" ...)
Property Changed (... "MyOtherProperty" ...)
Whether this is "correct" or not depends upon the purpose of these events.
If you want to test the number of events that gets raised, and the order they get raised in, you can easily extend your existing test:
[TestMethod]
public void Test_ThatMyEventIsRaised()
{
List<string> receivedEvents = new List<string>();
MyClass myClass = new MyClass();
myClass.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
{
receivedEvents.Add(e.PropertyName);
};
myClass.MyProperty = "testing";
Assert.AreEqual(2, receivedEvents.Count);
Assert.AreEqual("MyProperty", receivedEvents[0]);
Assert.AreEqual("MyOtherProperty", receivedEvents[1]);
}

If you're doing TDD then event testing can start to generate a lot of repetitive code. I wrote an event monitor that enables a much cleaner approach to unit test writing for these situations.
var publisher = new PropertyChangedEventPublisher();
Action test = () =>
{
publisher.X = 1;
publisher.Y = 2;
};
var expectedSequence = new[] { "X", "Y" };
EventMonitor.Assert(test, publisher, expectedSequence);
Please see my answer to the following for more details.
Unit testing that an event is raised in C#, using reflection

This is very old and probably wont even be read but with some cool new .net features I have created an INPC Tracer class that allows that:
[Test]
public void Test_Notify_Property_Changed_Fired()
{
var p = new Project();
var tracer = new INCPTracer();
// One event
tracer.With(p).CheckThat(() => p.Active = true).RaisedEvent(() => p.Active);
// Two events in exact order
tracer.With(p).CheckThat(() => p.Path = "test").RaisedEvent(() => p.Path).RaisedEvent(() => p.Active);
}
See gist: https://gist.github.com/Seikilos/6224204

Below is a slightly changed Andrew's code which instead of just logging the sequence of raised events rather counts how many times a specific event has been called. Although it is based on his code I find it more useful in my tests.
[TestMethod]
public void Test_ThatMyEventIsRaised()
{
Dictionary<string, int> receivedEvents = new Dictionary<string, int>();
MyClass myClass = new MyClass();
myClass.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
{
if (receivedEvents.ContainsKey(e.PropertyName))
receivedEvents[e.PropertyName]++;
else
receivedEvents.Add(e.PropertyName, 1);
};
myClass.MyProperty = "testing";
Assert.IsTrue(receivedEvents.ContainsKey("MyProperty"));
Assert.AreEqual(1, receivedEvents["MyProperty"]);
Assert.IsTrue(receivedEvents.ContainsKey("MyOtherProperty"));
Assert.AreEqual(1, receivedEvents["MyOtherProperty"]);
}

Based on this article, i have created this simple assertion helper :
private void AssertPropertyChanged<T>(T instance, Action<T> actionPropertySetter, string expectedPropertyName) where T : INotifyPropertyChanged
{
string actual = null;
instance.PropertyChanged += delegate (object sender, PropertyChangedEventArgs e)
{
actual = e.PropertyName;
};
actionPropertySetter.Invoke(instance);
Assert.IsNotNull(actual);
Assert.AreEqual(propertyName, actual);
}
With this method helper, the test becomes really simple.
[TestMethod()]
public void Event_UserName_PropertyChangedWillBeFired()
{
var user = new User();
AssertPropertyChanged(user, (x) => x.UserName = "Bob", "UserName");
}

Don't write a test for each member - this is much work
(maybe this solution is not perfect for every situation - but it shows a possible way. You might need to adapt it for your use case)
It's possible to use reflection in a library to test if your members are all responding to your property changed event correctly:
PropertyChanged event is raised on setter access
Event is raised correct (name of property equals argument of raised event)
The following code can be used as a library and shows how to test the following generic class
using System.ComponentModel;
using System.Linq;
/// <summary>
/// Check if every property respons to INotifyPropertyChanged with the correct property name
/// </summary>
public static class NotificationTester
{
public static object GetPropertyValue(object src, string propName)
{
return src.GetType().GetProperty(propName).GetValue(src, null);
}
public static bool Verify<T>(T inputClass) where T : INotifyPropertyChanged
{
var properties = inputClass.GetType().GetProperties().Where(x => x.CanWrite);
var index = 0;
var matchedName = 0;
inputClass.PropertyChanged += (o, e) =>
{
if (properties.ElementAt(index).Name == e.PropertyName)
{
matchedName++;
}
index++;
};
foreach (var item in properties)
{
// use setter of property
item.SetValue(inputClass, GetPropertyValue(inputClass, item.Name));
}
return matchedName == properties.Count();
}
}
The tests of your class can now be written as. (maybe you want to split the test into "event is there" and "event raised with correct name" - you can do this yourself)
[TestMethod]
public void EveryWriteablePropertyImplementsINotifyPropertyChangedCorrect()
{
var viewModel = new TestMyClassWithINotifyPropertyChangedInterface();
Assert.AreEqual(true, NotificationTester.Verify(viewModel));
}
Class
using System.ComponentModel;
public class TestMyClassWithINotifyPropertyChangedInterface : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
private int id;
public int Id
{
get { return id; }
set { id = value;
NotifyPropertyChanged("Id");
}
}
}

I've made an extension here:
public static class NotifyPropertyChangedExtensions
{
private static bool _isFired = false;
private static string _propertyName;
public static void NotifyPropertyChangedVerificationSettingUp(this INotifyPropertyChanged notifyPropertyChanged,
string propertyName)
{
_isFired = false;
_propertyName = propertyName;
notifyPropertyChanged.PropertyChanged += OnPropertyChanged;
}
private static void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == _propertyName)
{
_isFired = true;
}
}
public static bool IsNotifyPropertyChangedFired(this INotifyPropertyChanged notifyPropertyChanged)
{
_propertyName = null;
notifyPropertyChanged.PropertyChanged -= OnPropertyChanged;
return _isFired;
}
}
There is the usage:
[Fact]
public void FilesRenameViewModel_Rename_Apply_Execute_Verify_NotifyPropertyChanged_If_Succeeded_Through_Extension_Test()
{
// Arrange
_filesViewModel.FolderPath = ConstFolderFakeName;
_filesViewModel.OldNameToReplace = "Testing";
//After the command's execution OnPropertyChanged for _filesViewModel.AllFilesFiltered should be raised
_filesViewModel.NotifyPropertyChangedVerificationSettingUp(nameof(_filesViewModel.AllFilesFiltered));
//Act
_filesViewModel.ApplyRenamingCommand.Execute(null);
// Assert
Assert.True(_filesViewModel.IsNotifyPropertyChangedFired());
}

Related

C# Trigger RaisePropertyChanged of Parent from Child?

I have this code where I have my ViewModel and the ViewModel has a property where it gets all of its properties.
This is rough pseudo-code:
public class MyClassViewModel : INotifyPropertyChanged
{
public MyClassViewModel ()
{
}
public BaseClass myClassBase { get ; set; }
public string Title
{
get
{
return myClassBase.Title;
}
set
{
myClassBase.Title = value;
RaisePropertyChanged("Title");
}
}
public string Description
{
get
{
return myClassBase.Description;
}
set
{
myClassBase.Description = value;
RaisePropertyChanged("Description");
}
}
}
And this is the BaseClass:
public class BaseClass
{
public BaseClass()
{
}
public string Title {get;set;}
public string Description {get;set;}
}
CheckItemViewModel is the one binded to UI. So if I do something like MyClassViewModel .Title = "Test"; it properly refreshes the UI.
However, I need to do something like MyClassViewModel.myClassBase.Title = "Test" for specific reasons (Javascript - Chakra interface). The problem with this then is that the UI does not Refresh anymore since it doesn't have RaisePropertyChanged.
Even when I implemented RaisePropertyChanged inside the BaseClass itself, it still doesn't work. It doesn't work because PropertyChanged in BaseClass is always null.
I suspect it's because MyClassViewModel is the one binded to UI. So PropertyChanged in BaseClass is never binded.
Is there a way to trigger the Parent's RaisePropertyChanged?
Thank you
I would suggest implementing INotifyPropertyChanged on both classes, then have MyClassViewModel subscribe to the event in BaseClass and forward it to the UI:
public class MyClassViewModel : INotifyPropertyChanged, IDisposable
{
private BaseClass myClassBase;
public void Dispose()
{
if (myClassBase != null) myClassBase.PropertyChanged -= OnBaseClassPropertyChanged;
}
public BaseClass MyClassBase {
get {
return myClassBase;
}
set {
if (myClassBase != null) myClassBase.PropertyChanged -= OnBaseClassPropertyChanged;
myClassBase = value;
myClassBase.PropertyChanged += OnBaseClassPropertyChanged;
}
}
private void OnBaseClassPropertyChanged(object sender, PropertyChangedEventArgs args) {
RaisePropertyChanged(args.PropertyName);
}
// forwarded properties (Title and Description) go here
}
First of all, you can simplify the RaisePropertyChanged this way:
public void RaisePropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
So you don't need to write RaisePropertyChanged("Description"), but only: RaisePropertyChanged(), and the propertyName is automatically injected. That's awesome if you refactor frequently: you don't have to deal with the nightmare of remembering all the "Title" and "Description" strings in the whole solution :)
Second, if the BaseClass has the PropertyChangedEvent, you can listen to it in the MyClassViewModel.
myClassBase.PropertyChanged += (s, e) => { RaisePropertyChanged(e.PropertyName); };
But, if you don't inject myClassBase immediately in the constructor of MyClassViewModel, or if the myClassBase can change sometime, things get a bit more complicated.
You have to make MyClassViewModel also to implement INotifyPropertyChanging:
public event PropertyChangingEventHandler PropertyChanging;
public void RaisePropertyChanging([CallerMemberName] string propertyName = null)
{
PropertyChanging?.Invoke(this, new PropertyChangingEventArgs(propertyName));
}
You have to raise notifications also for the myClassBase:
public BaseClass myClassBase
{
get { return _myClassBase; }
set
{
RaisePropertyChanging();
_myClassBase = value;
RaisePropertyChanged();
}
}
private BaseClass _myClassBase;
Then, all you need is this code:
public MyClassViewModel()
{
PropertyChanging += OnPropertyChanging;
PropertyChanged += OnPropertyChanged;
}
private void OnPropertyChanging(object sender, PropertyChangingEventArgs e)
{
if (e.PropertyName != nameof(MyClassViewModel.myClassBase))
return; //or do something with the other properties
if (myClassBase == null)
return;
myClassBase.PropertyChanged -= OnMyBaseClassPropertyChanged;
}
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName != nameof(MyClassViewModel.myClassBase))
return; //or do something with the other properties
if (myClassBase == null)
return;
myClassBase.PropertyChanged += OnMyBaseClassPropertyChanged;
}
private void OnMyBaseClassPropertyChanged(object sender, PropertyChangedEventArgs e)
{
RaisePropertyChanged(e.PropertyName);
}
NB: I use the C#-6.0 nameof() operator, I hope you can use it, it's simply awesome!
EDIT:
Here you have a simple test method that demonstrates the correct functionality:
[TestMethod]
public void ChildClassPropertyChanged()
{
var bc = new BaseClass();
var c = new MyClassViewModel();
bc.Title = "t1";
c.myClassBase = bc;
Assert.AreEqual("t1", c.Title);
c.Title = "t2";
Assert.AreEqual("t2", c.Title);
c.myClassBase.Title = "t3";
Assert.AreEqual("t3", c.Title);
c.myClassBase = new BaseClass();
bc.Title = "t4";
Assert.AreEqual(null, c.Title);
c.myClassBase.Title = "t5";
Assert.AreEqual("t5", c.Title);
}
Keep in mind that if you set a null myClassBase, inside your properties' getters and setters the code throws a NullReferenceException. Maybe you should modify it this way:
public string Title
{
get
{
return myClassBase?.Title;
}
set
{
if (myClassBase != null)
myClassBase.Title = value;
RaisePropertyChanged();
}
}

Why WeakEventManager does not fire an event when the sender is not the nominal?

I don't like off-the-standard pattern, but I was making a quick test on my app, and I bumped against this strange behavior.
Consider a normal class exposing an event, here the very common PropertyChanged, but I think could be any other.
The subscriber chooses to subscribe the event via the WeakEventManager helper. Now, the "odd" thing is the actual sender reference: as long the instance is the same as was used on the subscription, everything goes fine. However, when you use another object, no notification will be issued.
Again, that's NOT a good pattern, but I wonder whether there is any good reason for this limitation, or rather that is a kind a bug. More a curiosity than a real need.
class Class1
{
static void Main(string[] args)
{
var c = new MyClass();
WeakEventManager<INotifyPropertyChanged, PropertyChangedEventArgs>.AddHandler(
c,
"PropertyChanged",
Handler
);
c.ActualSender = c;
c.Number = 123; //will raise
c.ActualSender = new Class1();
c.Number = 456; //won't raise
Console.ReadKey();
}
static void Handler(object sender, PropertyChangedEventArgs e)
{
Console.WriteLine("Handled!");
}
}
class MyClass : INotifyPropertyChanged
{
public object ActualSender { get; set; }
private int _number;
public int Number
{
get { return this._number; }
set
{
if (this._number != value)
{
this._number = value;
this.OnPropertyChanged("Number");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(
string name
)
{
this.PropertyChanged(
this.ActualSender,
new PropertyChangedEventArgs(name)
);
}
}
EDIT: here is a rough way to achieve the expected behavior (hard-links for sake of simplicity).
class Class1
{
static void Main(string[] args)
{
var cx = new MyClass();
var cy = new MyClass();
Manager.AddHandler(cx, Handler1);
Manager.AddHandler(cx, Handler2);
Manager.AddHandler(cy, Handler1);
Manager.AddHandler(cy, Handler2);
cx.ActualSender = cx;
cx.Number = 123;
cx.ActualSender = new Class1();
cx.Number = 456;
cy.ActualSender = cy;
cy.Number = 789;
cy.ActualSender = new Class1();
cy.Number = 555;
Console.ReadKey();
}
static void Handler1(object sender, PropertyChangedEventArgs e)
{
var sb = new StringBuilder();
sb.AppendFormat("Handled1: {0}", sender);
var c = sender as MyClass;
if (c != null) sb.AppendFormat("; N={0}", c.Number);
Console.WriteLine(sb.ToString());
}
static void Handler2(object sender, PropertyChangedEventArgs e)
{
var sb = new StringBuilder();
sb.AppendFormat("Handled2: {0}", sender);
var c = sender as MyClass;
if (c != null) sb.AppendFormat("; N={0}", c.Number);
Console.WriteLine(sb.ToString());
}
}
static class Manager
{
private static Dictionary<object, Proxy> _table = new Dictionary<object, Proxy>();
public static void AddHandler(
INotifyPropertyChanged source,
PropertyChangedEventHandler handler
)
{
var p = new Proxy();
p._publicHandler = handler;
source.PropertyChanged += p.InternalHandler;
_table[source] = p;
}
class Proxy
{
public PropertyChangedEventHandler _publicHandler;
public void InternalHandler(object sender, PropertyChangedEventArgs args)
{
this._publicHandler(sender, args);
}
}
}
I haven't found any documentation that says anything about this, but you can look at the WeakEventManager source code to see why this happens.
The manager keeps a table mapping the registered source objects to its handlers. Note that this source object is the one you pass in when adding the handler.
When the manager receives an event, it looks up the relevant handlers from this table, using the sender of the event as key. Obviously, if this sender is not the same as the one that is registered, the expected handlers will not be found.
Edit
Below is some pseudo-code to illustrate.
public class PseudoEventManager : IWeakEventListener
{
private static PseudoEventManager _instance = new PseudoEventManager();
private readonly Dictionary<object, List<object>> _handlerTable
= new Dictionary<object, List<object>>();
public bool ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
{
foreach (var handler in _handlerTable[sender]) // point of interest A
//invoke handler
}
public static void AddHandler(object source, object handler)
{
if (!_instance._handlerTable.ContainsKey(source))
_instance._handlerTable.Add(source, new List<object>()); //point of interest B
_instance._handlerTable[source].Add(handler);
//attach to event
}
}
When adding a handler, the source you pass in gets added to a lookup table. When an event is received, this table is queried for the sender of this event, to get the relevant handlers for this sender/source.
In your sample, the source you're listening on is c, which the first time is also the value for ActualSender. Thus, the sender of the event is the same as the registered source, which means the handler is correctly found and called.
The second time however, the ActualSender is a different instance than c. The registered source doesn't change, but the value of the sender parameter is now different! Hence it won't be able to retrieve the handlers, and nothing gets called.

bind listbox to index C#

I dont know if it possible but what I want is something like that
In WinForm listbox1 has a list of lines(copied from file)
In another Thread and class I run on a List that contains the same lines each line I parse and DoSomething
once I finish with that line I want the index in the listbox to change
from my basic and limited understanding my approach was with an Event to fire in form and than maybe using Invoke (for not to cross thread )
Is there is a way to somehow bind to index of the listbox somehow with my for/foreach loop ?
class form
{
listBoxScript.SetSelected(ScriptCounter, true);<--bind the ScriptCounter?
}
in another Class
class RunScript
{
//..
public void RunScriptList()
{
ScriptCounter = 0 ;
foreach ( var cell in ScriptList)
{
ScriptCounter ++;
//DoSomething
}
}
}
Make sure you implement INotifyPropertyChanged in RunScript class. Here's a complete sample:
class RunScript : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int scriptCounter;
private ISynchronizeInvoke invoker;
public RunScript(ISynchronizeInvoke invoker)
{
if (invoker == null) throw new ArgumentNullException("invoker");
this.invoker = invoker;
}
public async void RunScriptList()
{
ScriptCounter = 0;
foreach (var cell in Enumerable.Range(1, 15))
{
ScriptCounter++;
await Task.Delay(1000);
//DoSomething
}
}
public int ScriptCounter
{
get { return scriptCounter; }
set
{
scriptCounter = value;
OnPropertyChanged();
}
}
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
{
Action action = () => handler(this, new PropertyChangedEventArgs(propertyName));
invoker.Invoke(action, null);
}
}
}
private RunScript rs;
public Form1()
{
InitializeComponent();
rs = new RunScript(this)
var binding = new Binding("SelectedIndex", rs, "ScriptCounter", false, DataSourceUpdateMode.OnPropertyChanged);
listBox1.DataBindings.Add(binding);
}
private void button1_Click(object sender, EventArgs e)
{
rs.RunScriptList();
}
Note I have used async/await in RunScriptList, If you do it in another thread you need to fire PropertyChanged event in main thread to avoid cross thread exception.

Polling an object's public variable

I would like to notify a program immediately when there is a change in a bool variable that is a public variable of an object. For example;
say, an instance of class conn is created within a windows form application.
there is a Ready variable, a public variable of the class conn is present.
I would like to get notified whenever there is a change in this variable.
I did a quick research to solve this problem within stackoverflow but the answers suggested the use of property, which, I think is not suitable for my application.
I will assume you are referring to a field when you say public variable.
With few exceptions, it is preferable to not have public fields in C# classes, but rather private fields with public accessors:
class BadClass
{
public int Value; // <- NOT preferred
}
class GoodClass
{
private int value;
public int Value
{
get { return this.value; }
set { this.value = value; }
}
}
One of the reasons to structure your code this way is so you can do more than one thing in the property's getter and setters. An example that applies to your scenario is property change notification:
class GoodClass : INotifyPropertyChanged
{
private int value;
public int Value
{
get { return this.value; }
set
{
this.value = value;
this.OnPropertyChanged("Value");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string name)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(name);
}
}
}
If you were to implement your class like this, you could use it this way:
void SomeMethod()
{
var instance = new GoodClass();
instance.PropertyChanged += this.OnPropertyChanged;
}
void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "Value")
{
// Do something here.
}
}
If you change the Value property, not only will it change the value of the underlying field, but it will also raise the PropertyChanged event, and call your event handler.
You want to use the Observer pattern for this. The most straight forward way to do this in .NET is the event system. In the class conn, create an event:
public event EventHandler ReadyChanged;
and then when you create an instance of conn, subscribe to that event:
o.ReadyChanged += (s, e) =>
{
// do something
}
and then finally, when the flag changes in conn, fire the event via a new method named OnReadyChanged:
protected virtual void OnReadyChanged()
{
if (ReadyChanged != null) { ReadyChanged(this, new EventArgs()); }
}

How to write a Trigger?

I want my C# code to call an event whenever a value is assigned to my object.
How exactly would I need to go about that?
class MyClass {
ManualResetEvent mre;
public MyClass() {
mre = new ManualResetEvent(false);
Data = null;
}
public object Data { get; set; }
void DataSet(object sender, EventArgs e) {
Console.WriteLine("object Data has been set.");
mre.Set();
}
}
Delegates don't seem to be what I need. An event, maybe? How would I write such an event, if so?
MyClass mc;
void processA() {
mc = new MyClass();
mc.Data = GetDataFromLongProcess();
}
private object data;
public object Data {
get { return data;}
set {
if(value != data) {
data = value;
OnDataChanged();
}
}
}
protected virtual void OnDataChanged() {
EventHandler handler = DataChanged;
if(handler != null) handler(this, EventArgs.Empty);
}
public event EventHandler DataChanged;
then hook any code to the DataChanged event. For example:
MyClass mc = ...
mc.DataChanged += delegate {
Console.WriteLine("new data! wow!");
};
If you want to fire an event when your property is set, you would do something like this:
public event Action OnDataChanged;
protected object _data = null;
public object Data
{
get { return _data; }
set
{
_data = value;
if(OnDataChanged != null)
OnDataChanged();
}
}
Then you would simply wire up event handlers to your object like so:
mc = new MyClass();
mc.OnDataChanged += delegate() { Console.WriteLine("It changed!"); };
mc.Data = SomeValue();
I think you're on the right track with an event-based model. Also take a look at the Observer pattern (which is the basis for .Net delegates and events underneath it all, as I understand):
http://www.dofactory.com/Patterns/PatternObserver.aspx
But the bottom line, as the other useful answer so far (Mr. Gravell's implementation) indicates, you're going to have to have code IN the setter to get it hooked up. The only alternative would be to poll the value for changes, which just smells bad to me.
you could implement INotifyPropertyChanged (this is more or less a event) or you could take your class a Action (Trigger) and call this, whenn the property changed.
Just don't use automatic properties but a concrete setter and call your event/trigger from there.
Conceptually, you would define an event in your class, and in your property set blocks, you would invoke the event with the necessary arguments to determine what just happened.
public event SomeDelegateThatTakesIntAsParameter myEvent;
void SetData(int data)
{
if(myEvent!= null)
myEvent(data)
}

Categories

Resources