none
C# deleate event - how do you say it in VBA ? RRS feed

  • Question

  • Hi

    My Test class attach handler as CommPort.MyEvent += MyHandler;     (C#)

    CommPort is the class being tested. It generates events.

    How should i code the said statement  in VBA using AddressOf ?

    CommPort class header

       [ClassInterface(ClassInterfaceType.AutoDual)]
        public class CommPort : PortParamIf
        {
            .....
    
            public delegate void MyDelegate(string s);
            public static event MyDelegate MyEvent;
    
            .....
    
           public static void OnMyEvent()
            {
                if (MyEvent != null && RxData.Length > 1 )
                {
                     MyEvent(RxData); 
                }
            }
    
    
            

    Seems that MyEvent is not exposed to VBA, how do i rectify it ?

    Thanks


    • Edited by BR-99 Monday, June 3, 2013 4:11 PM
    Monday, June 3, 2013 3:54 PM

Answers

  • You need to mark it the appropriate type for the main class, too:

        // ClassInterface was wrong
        [ClassInterface(ClassInterfaceType.AutoDual)]
        [ComSourceInterfaces(typeof(EventsIF))]  //Our event source is EventsIF interface
        public class CommPort : PortParamIf ,  EventsIF


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Mike FengModerator Tuesday, June 4, 2013 1:13 AM
    • Marked as answer by BR-99 Tuesday, June 4, 2013 9:21 AM
    Monday, June 3, 2013 9:16 PM
    Moderator

All replies

  • In order to make the event exposed to COM, you'll need to create a separate interface for the event(s). 

    See this MSDN blog post for details: http://blogs.msdn.com/b/murat/archive/2008/11/20/exposing-com-events-c.aspx


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Monday, June 3, 2013 4:21 PM
    Moderator
  • Thanks ! but ........... i now can "see' just OnMyEvent
    The  other interface is gone
    Here is what I did

    namespace CommPortNS
    {
        using System;
        using System.IO.Ports;
        using System.Runtime.InteropServices;
        using System.Threading;
    
    
        [ComVisible(true)] 
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] //Our managed interface will be IDispatch
    
        public interface EventsIF 
        {
              void OnMyEvent();
        }
    
    
        [ClassInterface(ClassInterfaceType.None)]
        [ComSourceInterfaces(typeof(EventsIF))]  //Our event source is EventsIF interface
    
        
        public class CommPort : PortParamIf ,  EventsIF
        {
    ............
    
           [ComVisible(false)]
            public delegate void MyDelegate(string s);
            public static event MyDelegate MyEvent;
        
        }
    
    
        interface PortParamIf
        {
             [System.Runtime.InteropServices.ComVisible(true)]
            void Go();
            string ReceivedData();
            void InitPort(string PortName, int BaudRate);
            void Write(string Data);
            void ListPortSetup();
        }
    }
    

    Can you please spot my mistake ?

    In VBA class  i have

    Private WithEvents COM_Event As CommPort.CommPort
    Private Sub COM_Event_OnMyEvent()
       MsgBox "COM_Event Fired ! "
    End Sub

    is this right ?

    Thanks

    Monday, June 3, 2013 7:46 PM
  • You need to mark it the appropriate type for the main class, too:

        // ClassInterface was wrong
        [ClassInterface(ClassInterfaceType.AutoDual)]
        [ComSourceInterfaces(typeof(EventsIF))]  //Our event source is EventsIF interface
        public class CommPort : PortParamIf ,  EventsIF


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by Mike FengModerator Tuesday, June 4, 2013 1:13 AM
    • Marked as answer by BR-99 Tuesday, June 4, 2013 9:21 AM
    Monday, June 3, 2013 9:16 PM
    Moderator
  • Thanks a lot
    Tuesday, June 4, 2013 9:21 AM