AddHandler syntax error

BodeTech
BodeTech used Ask the Experts™
on
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
Comment
Watch Question

Do more with

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

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

Try:
   AddHandler currentDomain.UnhandledException, AddressOf MYExceptionHandler
or
   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).

Author

Commented:
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.
Commented:
PAQd, 500 points refunded - asker posted their solution

GhostMod
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