I am trying to declare a variable in a VB6 module as follows:
Public WithEvents MyObject As MyClass
The help files say that WithEvent
I'm not sure how large your application is, but there are several ways to do this. Here's what I would do, though this may take you 15 minutes or so to refactor your app to use it.
First copy all of the Public and Global methods and members from your Module into a class called (for example) clsApplication.
Second, in the now-empty Module (lets call it modGlobal), declare Public Property Get Application() As clsApplication
. Go ahead and add a setter too.
Third, in your Sub Main()
that starts your program, add this as your first line Set modGlobal.Application = New clsApplication
.
Now you have replaces your module with a global class that can listen to events that occur on an application-wide basis. In the rest of your app, you can acccess your global state like this Application.Config
or Application.GetUser()
or whatever you else you keep at the global level.
You could of course apply this to just the variable that you wish to use WithEvents on, but you should really look to getting your code out of modules anyway.
Just saw the comments to @Mark. The minumal approach is what I refrered to last. If you event class is MyEventSource then create a class called MyEventSourceListener with a property called Target
that you pass the object into and is declared privately WithEvents. Then MyEventSourceListener can receive the events and forward them back to your module.
I don't like it because its a hack and puts the code back into the module, but it might be the most expedient approach.