Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# property setter not called when assiging the same value

I have a property ggFileName defined like this:

    private string _ggFileName = "";
    public string ggFileName
    {
        get 
        { 
            return _ggFileName;
        }
        set 
        {
            _ggFileName = value;
            ReadXmlSchemaFromFile();
        }
    }

When assinging a value to ggFileName, the method ReadXmlSchemaFromFile(); is called. So far so good.

My problem is that when I assign the property ggFileName with the same value it already contains, nothing happens. The setter is not called until I assign it a different value.

I agree that in almost every case this is perfectly logical, but in my case it raises a problem. What if the file in ggFileName is changed outside my application ? Assigning the same file again to ggFileName does not calls my setter, so ReadXmlSchemaFromFile() is also not called. So now I have to set a dummy file to property ggFileName and than assign the same file again to ggFileName to get it working.

C# seems to be wanting to help me by not calling the setter when assigning the same value, how can I tell c# to stop helping me ?

I did not know that c# did this, in all my setters I have code like this: if (value != _myValue) { do setter logic here } I guess that I do not need to write that check anymore than ?

Just to make things more clear as people do not seem to understand my question. 1. My setter does get called, but only when assigning a different value 2. I do not want to use a method, because after setting the property I want some checks to be done and I do not want any other user of my class to be able to bypas that check. that is what properties are for, or not ? 3. The code that assignes a value I cannot post. It is a property of a custom control and it is called by the designer by editing the object inspector. Again, it works as long as I keep feeding it different values

thanks

like image 622
GuidoG Avatar asked May 23 '14 11:05

GuidoG


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

For what it is worth - I came across this problem and in our codebase it was down to Fody.

Fody inserts an equality check and short circuits the setter. I didn't really expect your problem to be down to Fody (because of your mention of the visual designer). I thought it might be of value to others who come across this post as I did.

I used dotPeek to find the code being inserted.

For your benefit I used dotPeek to inspect the winforms PropertyGrid and found the code snippet below. The visual studio may well be different but I strongly suspect it is doing something similar. If you are really keen you could log a call stack when you set a different value. Somewhere in that call stack would be the function with the equality check.

private object SetPropertyValue(object obj, object objVal, bool reset, string undoText)
{
  DesignerTransaction designerTransaction = (DesignerTransaction) null;
  try
  {
    object propertyValueCore = this.GetPropertyValueCore(obj);
    if (objVal != null && objVal.Equals(propertyValueCore)) // if equal return!!!!
      return objVal;
    this.ClearCachedValues();

About Fody:

This technique of "weaving" in new instructions is fantastically powerful. You can turn simple public properties into full INotifyPropertyChanged implementations, add checks for null arguments, add Git hashes to your Assemblies, even make all your string comparisons case insensitive.

Worth noting it can be disabled with [DoNotCheckEquality].

like image 57
David Hollinshead Avatar answered Oct 31 '22 07:10

David Hollinshead


The problem is in the buggy designer from visual studio. When setting a property value using the "property windows" in designtime, this window does not set the value for you property when its the same value already in there.

When doing this in code there is no problem, than the value will be set no matter what.

This is another fine example how microsoft tries to help you (all be it helping you backwards in stead of forwards...)

like image 22
hirogen Avatar answered Oct 31 '22 05:10

hirogen