Simple question about set...
Does it only trigger when there is an assignment to the property or does it trigger with any change further on down the line? Like, if I have a property named MyProp in MyClass of type TestClass (assume testclass has a TestField field), will MyProp trigger the set code with a line like "MyProp.TestField = "cows are pretty"" or would it only trigger on something like "MyProp = MyOtherTestClass"
DannyPosted Apr 21, 2008, 11:03 PM
Let me explain a bit about what I'm working on:
This project is an employee scheduling program for a class I'm in. I have an employee class which contains an employee's general information as well as two properties pointing to different classes (their availibility and working schedules). My schedule classes are set up in that there is a base schedule class which contains methods to create an internal calendar. The working and availibility schedule classes inherit the base class so they can build calendars and assign to them separately. Within those schedule class's calendars, there are numerous instances of custom month, week, and day classes that I have made.
I need an "IsModified" flag to get thrown any time ANY of that is changed for an employee. Like, if I were to do something like:
"EmployeeCollection[4].Availibility.Months[1].Weeks[12].TimeBlocks[0].IsAvailible = true;"
...I need it to walk up from a very specific change several layers down of the availibility property to the main instance of an employee class. I can't seem to find a way to do it that isn't stupidly redundant, and if I know anything about programming it's that if something seems redundant, it's being done wrong.
Posted Apr 21, 2008, 10:19 PM
Then raise the event in the "set" accessor of the MyProp and it will notify any subscribers of the change in MyProp.
Something like:
public event EventHandler MyPropChanged;
private TestClass myProp;
public TestClass MyProp
{
set
{
//Set the value
myProp = value;
//If the changed event has subscribers
//then raise it.
if (MyPropChanged != null)
{
MyPropChanged(this, new EventArgs());
}
}
}
You could also create a set of event arguments and pass them as the type parameter of the generic EventHandler class.
DannyPosted Apr 21, 2008, 7:59 PM
AlanPosted Apr 21, 2008, 7:28 PM
In this line:
MyProp.TestField = "cows are pretty";
the MyClass.MyProp 'get' is first called which returns a TestClass object. The TestField property of that object is then 'set' to the string: "cows are pretty".
In this line:
MyProp = MyOtherTestClass;
the MyClass.MyProp 'set' is called and the backing field is set to the object, MyOtherTestClass.