AddHandler syntax error

BodeTech used Ask the Experts™
I have the following lines of code in my app (I put them in a module for global use)...I am getting a syntax error on the AddHandler (only that word is underlined), but when I look at the documentation, the syntax looks the same (see copied code below mine)

    'The 2 event handlers
    'add an unhandled exceptions handler
    Public currentDomain As AppDomain = AppDomain.CurrentDomain
    'for regular unhandled stuff
AddHandler currentDomain.UnhandledException, AddressOf Me.MYExceptionHandler
    'for threads behind forms
AddHandler Application.ThreadException, AddressOf Me.MYThreadHandler

    Private Sub MYExceptionHandler(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
          MsgBox("Handled Error") ' Just put tha there for the question

    End Sub

    Private Sub UHThreadEX(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)

           MsgBox("Unhandled Error") ' Just put tha there for the question

    End Sub

MS Code:

AddHandler MyObject.Event1, AddressOf Me.MyEventHandler
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

You can't use the keyword Me within a Module.

   AddHandler currentDomain.UnhandledException, AddressOf MYExceptionHandler
   AddHandler currentDomain.UnhandledException, AddressOf MyModuleName.MYExceptionHandler

You might also need to make your handler subs Public instead of Private (that is sometimes, but not always, necessary).


I got the correct answer in a MS forum.  Addhandlers have to be in a Sub or Function.  Once I moved it, I was fine.
PAQd, 500 points refunded - asker posted their solution

Community Support Moderator

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial