Solved

Property Notification

Posted on 2004-09-19
7
213 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 50 total points
Comment Utility

  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
Comment Utility
//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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 10

Expert Comment

by:smegghead
Comment Utility
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
Comment Utility
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
Comment Utility
so, what compile error did you get ?
0
 
LVL 5

Expert Comment

by:tzxie2000
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now