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

Trap Event of Class and tie to Method

P: n/a
I have a assembly from a third party that exposes an Event
I want to Trap the event and tie to a Method in a Class where I am consuming
their assembly. The class is actually a UserControl

How can I tie OnSettingsModified from the assembly to Method
Configuration_OnValid in my class ????

Thanks
Third Party App
============
public interface ISettings
{

event EventHandler OnSettingsModified;
}
public Class Settings: ISettings
{
private void textEdit_EditValueChanged(object sender, EventArgs e)
{
if (OnSettingsModified != null)
{
OnSettingsModified.Invoke(sender, e);
}

}

}

My App
======

Settings mySettings - new Settings();

private void Configuration_OnValid(object sender, System.EventArgs e)
{
UpdateEnableSave();
}
Nov 4 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Good morning sippyuconn.

The method of tying OnSettingsModified from the assembly to
Configuration_OnValid depends on how Settings implements the ISettings
interface: either explicitly or implicitly. I will go through each
possibility with you and provide the solution respectively.

=====================================
Possibility 1: 'Settings' IMPLICITLY implements 'ISettings'

public interface ISettings
{
event EventHandler OnSettingsModified;
}

public class Settings : ISettings
{
private void textEdit_EditValueChanged(object sender, EventArgs e)
{
if (OnSettingsModified != null)
{
OnSettingsModified.Invoke(sender, e);
}
}

#region ISettings Members

public event EventHandler OnSettingsModified;

#endregion
}

The code "public event EventHandler OnSettingsModified;" tells that
'Settings' is implicitly implementing 'ISettings', and the event is a part
of the class, so we can add the event handler by writing this code:

Settings mySettings = new Settings();
mySettings.OnSettingsModified += new EventHandler(Configuration_OnValid);

=====================================
Possibility 2: 'Settings' EXPLICITLY implements 'ISettings'

public class Settings : ISettings
{
public void textEdit_EditValueChanged(object sender, EventArgs e)
{
if (OnSettingsModified != null)
{
OnSettingsModified.Invoke(sender, e);
}
}

event EventHandler OnSettingsModified;

#region ISettings Members

event EventHandler ISettings.OnSettingsModified
{
add
{
lock (OnSettingsModified)
{
OnSettingsModified += value;
}
}
remove
{
lock (OnSettingsModified)
{
OnSettingsModified -= value;
}
}
}

#endregion
}

In this implementation, the event OnSettingsModified is not exposed as a
public event from the class 'Settings', thus, we cannot tie the event
handler in the same way as "possibility 1". The event is a part of the
interface, so the right way to add the event handler is:

Settings mySettings = new Settings();
ISettings iSettings = (ISettings)mySettings;
iSettings.OnSettingsModified += new EventHandler(Configuration_OnValid);

For more information about explicitly implementing an interface event,
please refer to the MSDN article:
http://msdn.microsoft.com/en-us/libr...46(VS.80).aspx
http://msdn.microsoft.com/en-us/libr...57(VS.80).aspx

sippyuconn, is the above information helpful to you? If it cannot help you
with the question, I appreciate it if you could share more implementation
of the class 'Settings' or send a small reproducible project to my mailbox:
ji****@microsoft.com

Have a very nice day!

Best Regards,
Jialiang Ge (ji****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

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.

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 5 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.