Solved

Property Notification

Posted on 2004-09-19
7
223 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

622 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