Skip to content

Instantly share code, notes, and snippets.

@yhaskell
Last active September 20, 2015 08:25
Show Gist options
  • Save yhaskell/eca34a464d91efa018d2 to your computer and use it in GitHub Desktop.
Save yhaskell/eca34a464d91efa018d2 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NotifyExample
{
class KCC
{
public event EventHandler<string> OnSettingChanged;
private string setting;
public string Setting
{
get { return setting; }
set { OnSettingChanged?.Invoke(this, value); setting = value; }
}
}
class Program
{
static void Main(string[] args)
{
var example = new KCC { Setting = "@yhaskell" };
example.OnSettingChanged += (s, e) => Console.WriteLine($"Setting changed to \"{e}\" from \"{((KCC)s).Setting}\"");
Console.WriteLine($"Name is {example.Setting}");
example.Setting = "Yohann Haskell, Esq. (not really)";
Console.WriteLine($"Name is now {example.Setting}");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NotifyExample
{
public class ChangeEventArgs<T> : EventArgs
{
public T Original { get; private set; }
public T Value { get; private set; }
public ChangeEventArgs(T from, T to)
{
this.Original = from;
this.Value = to;
}
}
class HasNotifiableProperties
{
public event EventHandler<ChangeEventArgs<string>> OnNameChanged;
private string name;
public string Name
{
get { return name; }
set { OnNameChanged(this, new ChangeEventArgs<string>(name, value)); name = value; }
}
}
class Program
{
static void Main(string[] args)
{
var example = new HasNotifiableProperties { Name = "@yhaskell" };
example.OnNameChanged += (s, e) => Console.WriteLine($"Name changed to \"{e.Value}\" from \"{e.Original}\"");
Console.WriteLine($"Name is {example.Name}");
example.Name = "Yohann Haskell, Esq. (not really)";
Console.WriteLine($"Name is now {example.Name}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment