Trap Event of Class and tie to Method

S

sippyuconn

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();
}
 
J

Jialiang Ge [MSFT]

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/library/ak9w5846(VS.80).aspx
http://msdn.microsoft.com/en-us/library/ms173157(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:
(e-mail address removed)

Have a very nice day!

Best Regards,
Jialiang Ge ([email protected], 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:
(e-mail address removed).

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top