none
Possible to not pass an parameter to an EventHandler that needs one? RRS feed

  • Question

  • OK. Relatively new to coding and thus far all my work has been in the VB.NET WinForms environment. We have a program that I have been asked to implement a new module into. The new module has a ADK so it should be easy. However, the demo code is written in C# (and runs on WPF). So translating is a lot of fun for a noob ;)

    My question is about an Event Handler. In the C# code, this is the Event Handler:

    // handle dispatch base events
    void EventHandler_DSPBASE(EventBase e)
    {
        try
        {
            ServiceEvent serviceEvent = e as ServiceEvent;
            if (serviceEvent == null)
                return;

            // more code here
        }
        catch (Exception exception)
        {
            // catch exceptions here
        }
    }

    On Initialized the EventHandler is set with this:

    // register dispatch handler
    ServiceBase servicebase = _oModule.GetService(ServiceType.DSPBASE);
    if (servicebase != null)
    {
        // handle data received from the dispatch base component
        servicebase.Register(EventHandler_DSPBASE);    // notice no parameter being passed but it works every time
        servicebase.SetDispatchBaseOption(12500, DispatchBase.ROUTE_DATA_RECEIVED);
    }

    Using VS 2015 I can run the demo code with no problems. When I translate this to VB.NET I get an error because the Event Handler requires the parameter (EventBase e). So in the Initialized section, this VB.NET code won't run.

    Dim servicebase As ServiceBase = _oModule.GetService(ServiceType.DSPBASE)
    If servicebase IsNot Nothing Then
        ' handle data received from the dispatch base component
        servicebase.Register(EventHandler_DSPBASE)    ' <- error here - red squiggly line in parenthesis
        servicebase.SetDispatchBaseOption(12500, DispatchBase.ROUTE_DATA_RECEIVED)
    End If

    Is there a reason that this code will run through C# but not through VB.NET? And is there a way I can set that Handler without passing the argument that the procedure requires in VB.NET?

    Any help will be appreciated.
    Thursday, July 13, 2017 5:43 PM

Answers

All replies

  • I think that the equivalent code in VB.NET is different:

       servicebase.Register( AddressOf EventHandler_DSPBASE) 

    Check if it works.

    Thursday, July 13, 2017 6:17 PM
  • Thank you for that suggestion. I will try that when I get back onto the machine that has the code. I will update this thread to indicate (fingers crossed) success :)
    Thursday, July 13, 2017 7:54 PM
  • Thank you for that suggestion. I will try that when I get back onto the machine that has the code. I will update this thread to indicate (fingers crossed) success :)
    Get back on the machine that has the code? You don't use a code repository like TFS or others?
    Thursday, July 13, 2017 8:29 PM
  • I apologize for not responding sooner. I ended up being sent on the road for some onsite work.

    Your suggestion was accurate and it fixed the issue I was having. Thank you for the help. It is very much appreciated.

    Wednesday, July 19, 2017 5:00 PM
  • @DA924x - Nope. As the noob coder I work off of one machine and anything i write is handed off to someone else to check. I guess they want to make sure I don't blow something up... #crashtheplanet or something :/

    • Edited by Noob_Coder Wednesday, July 19, 2017 5:03 PM added name
    Wednesday, July 19, 2017 5:02 PM