Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Property Notification

Posted on 2004-09-19
7
Medium Priority
?
234 Views
Last Modified: 2010-04-15
Hello, Is there a way I can create a property in a class (eg. public int m_nNumber), and somehow create an event handler which is called each time this property is changed?
0
Comment
Question by:auk_ie
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 10

Assisted Solution

by:eternal_21
eternal_21 earned 200 total points
ID: 12099011

  public event System.EventHandler NumberChanged;

  private int number;
  public int Number
  {
    get
    {
      return number;
    }
    set
    {
      if(number!=value)
      {
        number = value;
        if(NumberChanged!=null)
          NumberChanged(null, null);
      }
    }
  }
0
 
LVL 5

Accepted Solution

by:
tzxie2000 earned 800 total points
ID: 12099406
//define event args

  public class NumChangedEventArgs : EventArgs
  {
    private readonly int num;
    public int m_num
    {
      get{return num;}
    }
    public NumChangedEventArgs(int num)
    {
      this.num = num;
    }
  }

//define delegate
  public delegate void NumChangedEventHandler(object sender, NumChangedEventArgs e);

//define your class
  public class testclass
  {
    public testclass()
    {
      nNumber=0;
    }
    private int nNumber;
    public int m_nNumber
    {
      get{return nNumber;}
      set{nNumber=value;OnNumChanged(new NumChangedEventArgs(value));}//call the event
    }
    public event NumChangedEventHandler NumChanged;//event handle
    protected virtual void OnNumChanged(NumChangedEventArgs e)//event entry
    {
      if (NumChanged != null)
      {
        // Invokes the delegates. NumChangedEventArgs
        NumChanged(this, e);
      }
    }
  }

when using

you can write this in a button click event


private void button1_Click(object sender, System.EventArgs e)
{
   testclass test=new testclass();//new class
   test.NumChanged+=new NumChangedEventHandler(NumChanged);// assigned event
   test.m_nNumber=10;//change number
}

public void NumChanged(object sender, NumChangedEventArgs e)//event action
{
  MessageBox.Show(e.m_num.ToString());//show message
}
0
 

Author Comment

by:auk_ie
ID: 12099820
Nice answer tzxie2000 (a little elaborate for my needs but very good)

eternal_21, How would you write a handler for the public event System.EventHandler NumberChanged;

I tried

public void NumberChanged(object sender, NumChangedEventArgs e)//event action
{
  MessageBox.Show(number.ToString());
}

But that doen't compile

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:smegghead
ID: 12102737
just use System.EventArgs rather than NumChangedEventArgs

public void NumberChanged(object sender, System.EventArgs e)//event action
{
  MessageBox.Show(number.ToString());
}
0
 

Author Comment

by:auk_ie
ID: 12105493
Yes this is what I had,


public event System.EventHandler NumberChanged; //What does this line do.

//Duplicate declaration
public void NumberChanged(object sender, System.EventArgs e)//event action
{
  MessageBox.Show(number.ToString());
}
0
 
LVL 10

Expert Comment

by:smegghead
ID: 12105518
so, what compile error did you get ?
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12105716
I think you'd better use the full version of event
or you may simplely write a function to call it

//define a function
void NumberChanged(int number)
{
 //doing something
}//if in a class you can overwrite it for different usage

 public int Number
  {
    get
    {
      return number;
    }
    set
    {
        number = value;
        NumberChanged(number);
    }
  }
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question