Solved

Property Notification

Posted on 2004-09-19
7
219 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 10

Assisted Solution

by:eternal_21
eternal_21 earned 50 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 200 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
Technology Partners: 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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