How to use INotifyPropertyChanged?

Hi
In trying to view the value of a property of a an object and I'm not sure why this doesn't work.

1. I have a windows Form that instantiates a class called Counter and calls a member function DoCount that counts from 1 to 1000 and sets the value of a property named CurrentCount as it loops thru.
2. The class implements INotifyPropertyChange.
3. Textbox1 on the form is bound to the property CurrentCount.

Now when I run the application I should see the text in the text box change from 1 to 1000, but I dont. here is the class.

Q1. what should I do to be able to see the current value of the the property CurrentCount from the form? Q2. When I step thru this code the PropertyChanged event is always null in the FirePropertyChangedNotification method how do get that event to be fired?

public class Counter: INotifyPropertyChange
{
   public event PropertyChangedEventHandler PropertyChanged;  
   private int m_currentCount;
   public int CurrentCount
   {
      get { return m_currentCount; }
      set
      {
         m_currentCount = value;
         FirePropertyChangedNotification("CurrentCount");
      }
   }

   public void DoCount
   {
         for (int i = 0; i < 1000; i++)
                {
                    CurrentCount = i;
                }
   }
   private void FirePropertyChangedNotification(string propName)
   {
      if (PropertyChanged != null)
         PropertyChanged(this,
            new PropertyChangedEventArgs(propName));
   }  
}

LVL 1
vkarumbaiahAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

p_davisCommented:
private PropertyChangedEventHandler propertyChanged;
        public event PropertyChangedEventHandler PropertyChanged
        {
            add { this.propertyChanged += value;}
       
        }
0
PockyMasterCommented:
You got a mistake in the name of the interface.
Shouldn't it be INotifyPropertyChanged

You should not be able to see counting from 1 to 1000, this code counts from 0 to 999.

There could be something wrong in the databinding since you don't get the PropertyChanged event set.

Could you post your databind statement?
0
vkarumbaiahAuthor Commented:
Hi p_davis:
I think you are right however I still get PropertyChanged = null in the following code above

private void FirePropertyChangedNotification(string propName)
   {
      if (PropertyChanged != null)
         PropertyChanged(this,
            new PropertyChangedEventArgs(propName));
   }  

when does PropertyChanged  get set != null?
could you illustrate in a sample?
thanks

hi Rocky Master
yes you are right it should be INotifyPropertyChanged (sorry for the typo)
also yes it will count from 0 to 999
I haven't bound the object to anything yet I am trying to see if i can get the object to raise the event when the data is changed by it self.  
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

PockyMasterCommented:
I haven't bound the object to anything yet I am trying to see if i can get the object to raise the event when the data is changed by it self.  

--> no binding --> PropertyChanged = null
0
PockyMasterCommented:
To be more clear, there's no need to inform anybody when there's nobody listening.
PropertyChanged will become unequal to null when a listener gets attached
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vkarumbaiahAuthor Commented:
aah I see,
So this should be in my calling component? If not can you give me an example of how I would bind the property to the text box on my form? I was planning on using the Databindings property on the text box.

private PropertyChangedEventHandler propertyChanged;
        public event PropertyChangedEventHandler PropertyChanged
        {
            add { this.propertyChanged += value;}
       
        }
0
vkarumbaiahAuthor Commented:
I see what you meant
I did get the databinding to work by adding the following code to my form's constructor

 textBoxCount.DataBindings.Add("Text", _accountUpdaterController, "CurrentCount");

thank you both for your help
0
vkarumbaiahAuthor Commented:
I did not know how to bind to a text box once I realized the trick is to map the text box's Text property to the object's property it all fell in to place in my mind. thank you for your help
0
PockyMasterCommented:
exactly like 20738044 indeed :D
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.