Handling events from an object declared in a global module?

Naigewron

Member
Joined
Jul 23, 2007
Messages
5
Location
Bergen, Norway
Programming Experience
5-10
Hi!

I have a bit of a conundrum. I have a VB.net module where I declare some global variables (and since they're in a module, they're also shared). Now, I declared them like so:

VB.NET:
Module modGlobals

  Public WithEvents g_Customer As New Customer()
  Public WithEvents g_Ord As New Order()

End Module

The events they raise are declared and raised like so (simplified):

VB.NET:
' Declare eventhandler
Public Event CustomerUpdated As eventhandler

Public Sub UpdateCustomer(ByVal dt As DataTable)

  ' Do stuff and raise the event
  RaiseEvent CustomerUpdated(Me, New System.EventArgs)

End Sub

Now, I attempt to write an event handler for this event on one of my forms:

VB.NET:
Public Sub g_Customer_CustomerUpdated(ByVal sender As Object, ByVal e As System.EventArgs) Handles g_Customer.CustomerUpdated

  'Do stuff

End Sub

However, the compiler kindly notifies me that "Handles clause requires a WithEvents variable defined in the containing type or one of its base types."

Now, the g_Customer object is, as you can see, declared "WithEvents", and there is no way of declaring an instance of a module (nor would I want to). So, am I simply missing something, or can I not raise events in objects declared inside a module?

Thanks for any help and pointers!

Eskil
 
You can use the AddHandler statement in code to add the handler:
VB.NET:
AddHandler modGlobals.g_Customer.CustomerUpdated, AddressOf g_Customer_CustomerUpdated
Or you can keep your own WithEvents variable in your containing type/class pointing to the shared object:
VB.NET:
WithEvents myreference As Customer = modGlobals.g_Customer
The drawback of both these ways is if the shared variable is assigned a new instance your private handlers have to be reassigned.
 
You can use the AddHandler statement in code to add the handler:
VB.NET:
AddHandler modGlobals.g_Customer.CustomerUpdated, AddressOf g_Customer_CustomerUpdated

That worked very nicely, thank you! The purist in me would still like to use the "Handles" approach, but hey, I'm not restructuring my program for it.

Or you can keep your own WithEvents variable in your containing type/class pointing to the shared object:
VB.NET:
WithEvents myreference As Customer = modGlobals.g_Customer
The drawback of both these ways is if the shared variable is assigned a new instance your private handlers have to be reassigned.

Yeah, see this is where I miss my C++ pointers, hehehe :p
 
With the private WithEvents variable referencing the shared instance you would use Handles, in the case the shared instance was changed you just reassign the private variable pointing to the new reference also. It's part of the application design when using shared variables, it adds a level of synchronization similar to managing the flow of multiple forms or multiple threads and such.
 
True, but I'll be handling these events in multiple classes, and the object will be re-instanced a number of times, so having local copies in every class will just get very confusing and hard to maintain.

Appreciate your help though, cheers :)
 
If you wrote the class yourself and don't need the event to be instance dependent you can declare the event Shared. It solves synchronizing handlers between multiple subscribers when shared instance changes. You then use the AddHandler statement pointing to the class template event and not the instance event. Any instance of the class can raise this event and it routes to the same handlers.
 
Back
Top