By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,943 Members | 1,921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,943 IT Pros & Developers. It's quick & easy.

delegate andevent mechanisms

P: n/a
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.
Nov 20 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Wed, 19 Nov 2008 23:07:00 -0800, Gerhard <Ge*****@community.nospam>
wrote:
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.
A couple of quick MSDN searches turned up these articles:

http://msdn.microsoft.com/en-us/library/w369ty8x.aspx
http://msdn.microsoft.com/en-us/libr...39(VS.71).aspx
http://msdn.microsoft.com/en-us/library/3b8tbfec.aspx
http://msdn.microsoft.com/en-us/library/bzwdh01d.aspx
Nov 20 '08 #2

P: n/a
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,

Nov 21 '08 #3

P: n/a
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/libr...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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subs...#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/subs.../aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Nov 21 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.