I have a simple property within a Model class with a getter and setter
private bool _isThresholdBreached;
public bool IsThresholdBreached
{
get
{
return _isThresholdBreached;
}
set
{
if(_isThresholdBreached == value)
return;
_isThresholdBreached = value;
OnThresholdBreached?.Invoke(this, new EventArgs());
}
}
Strangely enough the property's value is getting changed without the setter getter hit. I have put a breakpoint both within the getter and setter and I see that when the getter is called on successive occasions the value has changed but the setter breakpoint never hit. How is this even possible ? Any pointers please.
EDIT : I have verified and can confirm that the backing field is not modified anywhere else in the class except in the setter
Your property has a backing field.
Any code calling the field directly would update that value without calling the setter (this could be both direct or via reflection).
I would start with Find usages of the _isThresholdBreached followed by full text search in the solution.
Related
I have a property, of a custom class, in C# that I have overridden the setter for. I want to compare a property of/in the custom class in the setter, like the following:
public DatabaseInfo CurrentDatabaseManagedSelection
{
get { return CurrentDatabaseManaged; }
set {
if (String.Equals(value.Name, CurrentDatabaseManaged.Name,StringComparison.OrdinalIgnoreCase))
return;
CurrentDatabaseManaged = DatabaseManagement.ReadDatabase(value.FileName);
}
}
Inside the DatabaseInfo class, there is a standard String property called Name.
However, when I run the program I get the following exception. Can anyone tell me why this happens and how to solve the issue please?
Exception has been thrown by the target of an invocation.
EDIT: I do set the value of the property which the setter above is for, in the constructor of the view model. I do this simply by setting CurrentDatabaseManagedSelection equal to an object of the DatabaseInfo class.
Think I might have found the problem... Well, I've solved it!
The issue was CurrentDatabaseManaged had not been initialized and so was equal to null when I tried setting the above property. I discovered this by adding a try.. catch in the setter method, and created a new String for CurrentDatabaseManaged.Name - the stack trace pointed to that line.
Hope that helps some one else in the future.
Usually, when I code property of classes that could be edited by the user with a binding of some kind... To prevent executing of GUI logic, I don't allow to assign the same value to the property:
public PMSAccountingYear AccountingYear{
get { return _accountingYear; }
set{
if(_accountingYear == value)
return;
_accountingYear = value;
NotifyOtherProperties();
LogChanges();
EmallToTheBoss();
Errr();
BlowBombInTheGarden();
Etc();
}
}
The condition check doesn't look elegantly, and cannot be detected with any automatic code analysis.
Can you please suggest a better case?
With one of the attributes maybe?
What you are doing is perfectly fine; this is the common way to implement setters for things like INotifyPropertyChanged (aee also the example on that page).
As the value did not change, there is no need to actually update the backing field and especially not to notify others of a “changed” value.
as I often let LinqToSql generate partial entity classes, I am wondering if my practice of adding additional properties via code is correct and if there is a better way of doing the same thing? I am also wondering what is the difference between accessing the values of other properties using this.PROPERTY_NAME vs _PROPERTY_NAME?
In my web app I keep using this.PROPERTY_NAME, but I am wondering if that is, as I already said in opening sentence, the proper approach I should be using. Also, What is _PROPERTY_NAME and when do we use it?
Example:
public partial class User
{
public bool IsThisProper {
get{
return this.SomeIntProperty == 10; // I usually use this
}
}
public bool WhenToUseThisApproach {
get{
return _SomeIntProperty == 10; // What is this in comparison to above?
}
}
}
One is the property, and the other is the private backing field in which that property stores it's value. If you want to execute whatever code the property has in it's getter/setter, then use the property, if you don't, then don't. Chances are you want to use the property, not the field, especially with setting (setting it triggers the property changed event, so about the only time to use the property is if you don't want that event raised).
I have a class with property say
private string fieldSelectedItem;
public string FieldSelectedItem
{
get
{
return fieldSelectedItem;
}
set
{
fieldSelectedItem = value;
}
}
it is accessed from many place.
I came across a situation that the a property in class is accessed by some event. and also some event is changing the value. i tried debugging. is it possible to check which event/function has changed/accessed the property. is there any method to do so.
How about placing a breakpoint in the setter and looking at the stack trace.
Simples.
The stack trace should give you some information about where the call has come from, if you break in the property accessors.
I have a control with a property public MyClass MyProperty{...} which value is shown on the screen as a graph. I want this property to be bindable to any other MyClass in the program by using the Binding class (MyProperty would be the propertyName parameter in this Binding constructor, and the other MyClass would be the dataMember parameter) .
MyClass implements INotifyPropertyChanged so on that side everything is all right. But it happens that if I don't implement a get accessor in MyProperty and try to bind something to it, I get a "Cannot bind to the property 'MyProperty' on the target control.
Parameter name: PropertyName" error.
Does this mean I have to implement a get accessor even if I know I will never need to read it's value and I want a OneWay (source to target) binding, and even if I just return null in the get accessor?
I'm guessing the Binding class uses this to compare the new value to the old one or to do some other internal stuff. I'm not sure, then, if it's a good idea to just return null, or it would be better to keep always a copy of whatever last object was assigned with the set accessor and return it in the get accessor. Maybe I really don't even need to write a get accessor and I'm doing something else wrong. It just happens that I get the error only when I comment out the get accessor and stop getting it when I put it back.
Edit: In case there is any confusion: When I say MyProperty's value is shown on the screen as a graph I don't mean it has a value that some other code reads and show in the screen. No one reads any value from MyProperty. MyProperty's set accessor is the one that draws stuff on the screen and that's the end of the cycle.
I'm not 100% sure I understand what you mean, but I think the exception you're encountering stems from the Binding class's CheckBinding function (reflectored):
if (descriptor.IsReadOnly && (this.controlUpdateMode != ControlUpdateMode.Never))
{
throw new ArgumentException(SR.GetString("ListBindingBindPropertyReadOnly", new object[] { this.propertyName }), "PropertyName");
}
Therefore, changing the Binding's ControlUpdateMode to ControlUpdateMode.Never may be what you're looking for