delegate andevent mechanisms

  • Thread starter Thread starter Gerhard
  • Start date Start date
G

Gerhard

Can you please give me an example showing best practices using the delegate
and event mechanisms built into the .Net framework so that changes to a
property would notify any interested listeners? Thanks.
 
Hello Gerhard,

Thank you for using Microsoft Managed Newsgroup Service, I'm Zhi-Xin Ye,
it's my pleasure to work with you on this issue.

To notify clients that a property value has changed, you can implement the
INotifyPropertyChanged interface on your classes.

For example,
 
Hello Gerhard,

Thank you for using Microsoft Managed Newsgroup Service, I'm Zhi-Xin Ye,
it's my pleasure to work with you on this issue.

To notify clients that a property value has changed, you can implement the
INotifyPropertyChanged interface on your classes.

For example,


============== Code Sample =================

class Customer : INotifyPropertyChanged
{
private int id;
private string name;

public Customer(int id, string name)
{
this.id = id;
this.name = name;
}

public int ID
{
get { return id; }
set
{
this.id = value;
NotifyPropertyChanged("ID");
}
}

public string Name
{
get { return name; }
set
{
this.name = value;
NotifyPropertyChanged("Name");
}
}

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}

#endregion
}

public partial class Form1: Form
{
public Form1()
{
InitializeComponent();

c.PropertyChanged += new
PropertyChangedEventHandler(c_PropertyChanged);
}

Customer c = new Customer(1, "aa");

private void button1_Click(object sender, EventArgs e)
{
c.Name = "ff";
}

private void button2_Click(object sender, EventArgs e)
{
c.ID = 2;
}

void c_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
MessageBox.Show(e.PropertyName + " Changed!");
}
}

==============================

For more information about the INotifyPropertyChanged Interface, you can
refer to this document:

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropert
ychanged.aspx

If anything is unclear, please feel free to let me know.

Have a nice day!

Best Regards,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top