Solved

Property Notification

Posted on 2004-09-19
7
216 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Library to convert HTML to PDF. 8 55
Data Saving(2) 2 41
Interview questions for support of a legacy ASP.NET site 4 37
Connection String 16 41
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

932 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

10 Experts available now in Live!

Get 1:1 Help Now