Trap Event of Class and tie to Method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?c2lwcHl1Y29ubg==?=

    #1

    Trap Event of Class and tie to Method

    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 OnSettingsModif ied from the assembly to Method
    Configuration_O nValid in my class ????

    Thanks


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

    event EventHandler OnSettingsModif ied;
    }


    public Class Settings: ISettings
    {
    private void textEdit_EditVa lueChanged(obje ct sender, EventArgs e)
    {
    if (OnSettingsModi fied != null)
    {
    OnSettingsModif ied.Invoke(send er, e);
    }

    }

    }

    My App
    ======

    Settings mySettings - new Settings();

    private void Configuration_O nValid(object sender, System.EventArg s e)
    {
    UpdateEnableSav e();
    }
  • Jialiang Ge [MSFT]

    #2
    RE: Trap Event of Class and tie to Method

    Good morning sippyuconn.

    The method of tying OnSettingsModif ied from the assembly to
    Configuration_O nValid 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 OnSettingsModif ied;
    }

    public class Settings : ISettings
    {
    private void textEdit_EditVa lueChanged(obje ct sender, EventArgs e)
    {
    if (OnSettingsModi fied != null)
    {
    OnSettingsModif ied.Invoke(send er, e);
    }
    }

    #region ISettings Members

    public event EventHandler OnSettingsModif ied;

    #endregion
    }

    The code "public event EventHandler OnSettingsModif ied;" 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.OnSe ttingsModified += new EventHandler(Co nfiguration_OnV alid);

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

    public class Settings : ISettings
    {
    public void textEdit_EditVa lueChanged(obje ct sender, EventArgs e)
    {
    if (OnSettingsModi fied != null)
    {
    OnSettingsModif ied.Invoke(send er, e);
    }
    }

    event EventHandler OnSettingsModif ied;

    #region ISettings Members

    event EventHandler ISettings.OnSet tingsModified
    {
    add
    {
    lock (OnSettingsModi fied)
    {
    OnSettingsModif ied += value;
    }
    }
    remove
    {
    lock (OnSettingsModi fied)
    {
    OnSettingsModif ied -= value;
    }
    }
    }

    #endregion
    }

    In this implementation, the event OnSettingsModif ied is not exposed as a
    public event from the class 'Settings', thus, we cannot tie the event
    handler in the same way as "possibilit y 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)mySe ttings;
    iSettings.OnSet tingsModified += new EventHandler(Co nfiguration_OnV alid);

    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:
    jialge@microsof t.com

    Have a very nice day!

    Best Regards,
    Jialiang Ge (jialge@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:
    msdnmg@microsof t.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.



    Comment

    Working...