AceInfinity
Emeritus, Contributor
Here's a nice quick example i've created, which utilizes an interface, events, delegates, and all that to raise events for notification's whenever a class instance's member variable changes:
I actually quite enjoy this kind of layout as well. It's all nice and clean, and there's lots of good stuff in this short example i've come up with.
(I'm fooling around in my C# IDE right now, so i've been posting random stuff today, but this is good for learning).
If you don't understand what is going on just post within this thread and i'll explain my code.
Note: If you'll notice, I have this:
This is mainly because if the value in this example is 5, and it's set to 5, then since it's the same value, it hasn't really changed to a different value, perceptively, although it has changed programatically, but I don't want to monitor it being changed to the same value here. If you want this, then just remove the if statement and you'll receive notifications for if it changes to the same value.
Here's the output:
:beerchug2:
C#:
private void MainMethod()
{
MyClass cls = new MyClass();
cls.VarChanged += new VariableNotifications.VarChangedEvent<int>(p_VarChanged);
MessageBox.Show("Now changing the ID member value for the instance of MyClass: cls");
cls.ID = 233;
}
public class MyClass : VariableNotifications
{
private int _ID;
public int ID
{
get { return _ID; }
set
{
if (_ID != value)
{
_ID = value;
RaiseChangedEvent("ID", _ID);
}
}
}
}
private void p_VarChanged(object sender, VarChangedEventArgs<int> e)
{
MessageBox.Show(string.Format("Member variable ({1}) from the class ({0}) has changed to a value of ({2})",
sender.GetType().Name, e.VarName, e.varValue));
}
public class VariableNotifications : IVariableChangedNotify<int>
{
public delegate void VarChangedEvent<T>(object sender, VarChangedEventArgs<T> value);
public event VarChangedEvent<int> VarChanged;
public void RaiseChangedEvent(string VarName, int VarValue)
{
VarChangedEvent<int> handle = this.VarChanged;
if (handle != null)
{
VarChangedEventArgs<int> VarArgs = new VarChangedEventArgs<int>(VarName, VarValue);
handle(this, VarArgs);
}
}
}
public class VarChangedEventArgs<T>
{
public string VarName { get; set; }
public T varValue { get; set; }
public VarChangedEventArgs(string varName, T args)
{
VarName = varName;
varValue = args;
}
}
public interface IVariableChangedNotify<T>
{
void RaiseChangedEvent(string VarName, T VarValue);
}
I actually quite enjoy this kind of layout as well. It's all nice and clean, and there's lots of good stuff in this short example i've come up with.
(I'm fooling around in my C# IDE right now, so i've been posting random stuff today, but this is good for learning).
If you don't understand what is going on just post within this thread and i'll explain my code.
Note: If you'll notice, I have this:
C#:
if (_ID != value)
{
_ID = value;
RaiseChangedEvent("ID", _ID);
}
This is mainly because if the value in this example is 5, and it's set to 5, then since it's the same value, it hasn't really changed to a different value, perceptively, although it has changed programatically, but I don't want to monitor it being changed to the same value here. If you want this, then just remove the if statement and you'll receive notifications for if it changes to the same value.
Here's the output:
:beerchug2:
Last edited: