none
How to fire evet from static-method RRS feed

  • Question

  • Hi

    I have C# class including events interface. The class instance is targeted to be used in VBA.

    The class has Read Thread static method which calls OnMyEvent non-static method to fire

    MyEvent.

          public static void Read()  // must be static
            {
                while (_continue )
                {
                    try
                    {
                        if (_init)
                        {
                            RxData = _serialPort.ReadLine();
                            OnMyEvent(RxData);  // <<<<<<<<<<< Error: An object reference is required for the non-static field, method, or property 
                         }
                    }
                    catch (TimeoutException) { }
                }
            }

    I searched the WEB but nothing solve  my issue. I  tried :

    Singleton  (but i have to "new" in VBA)

    pass arg by ref does not do it (same error)

    create new class instance (_inst.MyEvent) does not work

    The said Error moves around as i try to modify methods to non-static

    What do i do ?

    Your help will be appreciated

    Thanks

    Saturday, June 8, 2013 7:01 AM

Answers

  • Hi Br-99,

    It is impossible to call a non-static method in a static method. There is no way to achieve this goal.

    You need to re-design your business logic.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by BR-99 Thursday, June 13, 2013 7:02 PM
    Monday, June 10, 2013 7:53 AM
    Moderator

All replies

  • The data need to be defined as static like this

    static string RxData = ""; public static void Read() // must be static { RxData = _serialPort.ReadLine(); OnMyEvent(RxData); 

    }



    jdweng

    Saturday, June 8, 2013 9:58 AM

  • Thanks .... but the root cause is calling non-static method (OnMyEvent) from a
    static one (Read)

    Saturday, June 8, 2013 1:33 PM
  • Declare OnMyEvent as static. That will resolve the issue.

    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Saturday, June 8, 2013 1:59 PM
  • Thanks .... if i  do so, MyEvent (which must be non-static) will cause the said error,
    It just pushes the problem

    (MyEvent is used within OnMyEvent)

    Saturday, June 8, 2013 2:33 PM
  • I pushed the error forward

            public event MyEventHandler MyEvent = new MyEventHandler(OnMyEvent);
            public  static  void Trigger(string s)
            {
                
                if (MyEvent != null)
                {
                    if (RxData.Length > 1)
                    {
                        MyEvent(RxData); 
                    }
                }
            }
    
    Error: An object reference is required for the non-static field, method, 
    or property 'CommPortNS.CommPort.MyEvent'

    although i created MyEvent  instance - i'm still getting this error

    can you please help ?

    thanks

    Sunday, June 9, 2013 8:59 AM
  • with the following code i get clean build, however  MyEvent is  not updated 

        public  static  void Trigger(string s)
            {
                CommPort CP =new CommPort();
                if (CP.MyEvent != null)
                {
                    if (RxData.Length > 1)
                    {
                        CP.MyEvent(RxData); 
                    }
                }
                
            }

    this is my interface

       [ComVisible(true)]
        
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] //Our managed interface will be IDispatch
    
        public interface EventsIF
        {
            event MyEventHandler MyEvent;
        }
        

    what should i do in order to expose the event ?

    how to assign MyEvent = CP.MyEvent ?
     
    • Edited by BR-99 Sunday, June 9, 2013 2:05 PM
    Sunday, June 9, 2013 12:37 PM
  • Is it possible to declare EventsIFClass that's an abstract class that implements EventsIF, then put static declaraction of the event there?

    I think that's how a few VBA libaraies (including MS Office) do it.

    Monday, June 10, 2013 2:20 AM
    Answerer
  • Hi Br-99,

    It is impossible to call a non-static method in a static method. There is no way to achieve this goal.

    You need to re-design your business logic.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by BR-99 Thursday, June 13, 2013 7:02 PM
    Monday, June 10, 2013 7:53 AM
    Moderator