Reputation: 801
I'd like to trigger a method every-time a value is changed in my list.
The top answer on other questions related to this is to use a property, which I have (example below)
However, I think it would help me prevent any accidental bugs if I deny anything other than that property having access to the 'core' list it changes (for lack of better understanding what the term is) .
Below is my example property with a get and set accessor.
private List<Things>myPrivateList; // Only the Property should be able to access
private List<Things>MyPrivateList // Only the class its in can touch this
{
get { return myPrivateList; }
set
{
myPrivateList = value;
coolMethodThatNeedsToRunEverytime();
}
}
public List<Things>getMyPrivateList // Any class outside can read this
{
get { return myPrivateList; }
}
I might be going about this the wrong way though, it would useful to know if you have any suggestions for what I'm trying to achieve. Thanks very much in advance for any advice / example you may have.
Upvotes: 2
Views: 136
Reputation: 187
You can do what you want with one property :
public List<Things>MyPrivateList // Only the class its in can touch this
{
get { return myPrivateList; }
private set
{
myPrivateList = value;
coolMethodThatNeedsToRunEverytime();
}
}
Doing this, the setter will be private and the getter public
Upvotes: 1