Wednesday, March 14, 2012

Suddenly my application crashes without errors when using property binding

I introduced property binding in a previous blog post. Today, I introduce a problem when I write code of property binding.


Property binding is easy to mistake property names. If you mistake property name, you can't bind property correctly but also an application suddenly crashes without any errors.


So, please look at this code attentively. If you write such code, you can pass build action, but your application crashes suddenly without errors.

 public class ItemProperties : INotifyPropertyChanged  
 {  
   public event PropertyChangedEventHandler PropertyChanged;  
   public ItemProperties() { }  

   private string m_Text1;  
   public string Text1  
   {  
     get { return Text1; }  
     set  
     {  
       m_Text1 = value;  
       OnPropertyChanged("Text1");  
     }  
   }  

   protected void OnPropertyChanged(string name)  
   {  
     PropertyChangedEventHandler handler = this.PropertyChanged;  
     if (handler != null)  
       handler(this, new PropertyChangedEventArgs(name));  
   }  
 } 


The wrong code is here. If you write like this, your application suddenly shutdown.
get { return Text1; }  

You need to write like this.
get { return m_Text1; }  


I don't know why debugger can't catch error, but I noticed we need to pay attention when we use property binding.

7 comments:

  1. I dont see how this has to do with property binding? The Error is an eternal loop on the Get of Text1. But you are right the debugger or compiler should catch this. Maybe only with property binding this error results in a silent crash which are the most annoying crashes.

    ReplyDelete
  2. Are you certain that you're not getting a System.StackOverflowException? See Microsoft's official response to this issue here from 2008:

    http://stackoverflow.com/questions/2336847/why-doesnt-the-c-sharp-compiler-stop-properties-from-referring-to-themselves

    ReplyDelete
  3. The "Anonymous" user above is correct - this is not called "property binding" - it is "property recursion".

    ReplyDelete
  4. @Anonymous thanks your comment. We just talked about you said in Twitter. I also hope debugger catch internal stack overflow exception. So, please see my previous blog post the reason why I use property binding.(It's related my WP app)

    ReplyDelete
  5. @Dan Colasanti Thanks your comment and your twitter reply. Exactly, it's property recursion.I could get a whole sight in this problem. but I never got any errors from debugger.

    ReplyDelete
  6. The Joining Financial Contract can cope with two main areas: residence , resources and servicing.

    Property Management Companies

    ReplyDelete
  7. A large number of Arizona home owners will go through the property tax charm process this year because the evaluation section wrongly evaluated their properties value.

    Business for Sale Florida

    ReplyDelete