Click here to Skip to main content
15,891,903 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
for Example Let that i have a 'MyObject' class

C#
public class MyObject
{
    public string Name { get; set; }
}


if i want know if Name Property is Change Without Using
C#
public event PropertyChangedEventHandler PropertyChanged;

I will do My caller class Like This:

C#
public class MyCaller 
{
   private MyObject myObject;
   private string InitialName;
   private Thread myThread;  
   public event PropertyChangedEventHandler PropertyChanged;
   public MyCaller()
   {
      this.myObject = new MyObject();
      this.InitialName = this.myObject.Name;
      this.Thread  = new Thread(new ThreadStart(this.HandleName));
   } 
   
   private void HandleName()
   {
      while (true)
      {
        if (this.InitialName != this.myObject.Name)
        {
           this.InitialName = this.myObject.Name;
           if (this.PropertyChanged != null)
           {
              this.PropertyChanged(this, new PropertyChangedEventArgs("Name"));
           }
        }
      }
   }
} 


I Know that i can create PropertyChanged event on MyObject Class
but if i dont have the source code for MyObject Class I will do the another
solution.

My Question is there another way to solve this problem.


please, send me the answer at my Mail : [REMOVED EMAIL]

thank you
Posted
Updated 31-May-15 4:26am
v2
Comments
Dave Kreskowiak 31-May-15 10:28am    
Removed your email address from the post. NEVER put your email address in a public forum. All it does is attract the attention of spam bots that harvest addresses.

No, you're not going to get any "solution" sent to your email address. All interaction on this post MUST be done in this forum.

1 solution

It is impossible and makes no sense. If you want to handle some event triggered when some property of the instance of the type MyObject changes its value, you need to have the event PropertyChanged in this type and nowhere else. Not in MyCaller or some other type.

Saying "I don't have the source code for MyObject" sets a period in this story. You cannot use this type this way. However, you can do it in a derived class.

If the property MyObject.Name was virtual, you could override it in your class where you could add the PropertyChanged event, invoking it in the setter of MyObject.Name. That would, in turn, make your derived class sealing the invocation of this event, because any event instance can only be invoked in its declaring class, which can be considered as an important fool-proof feature. Please see my past answer:
UserControl custom event[^].

But you class MyObject, without changing its code, is completely closed for any modifications of its property behavior, due to its non-virtual declaration. Anyway, as a library class it would be useless in all respects. :-)

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900