We are migrating an application from C# to VB to meet our project’s needs but stumbled upon a problem with event handling in VB.
The application uses a COM Wrapper access a scanner in Silverlight. The object is created dynamically in the code, and an event is added to “AcquirePage”. This requires elevated trust of course.
Code in C#:
dynamic TwainSession;
(...)
TwainSession.AcquirePage += new AcquirePageDelegate(AcquirePageEventHandler);
As the only real “equivalent” of dynamic in VB is Object, we use:
Private TwainSession As Object
Everything is fine up to the point we want to handle an event of this Object. Because we are in Silverlight, we cannot have knowledge of the Object’s structure or events, hence the need to create it dynamically. In C# we simply use “+=” to add a handler to an event but:
AddHandler TwainSession.AcquirePage, AddressOf AcquirePageEventHandler
In VB gives: ‘AcquirePage’ is not an event of ‘Object’
Any way around that?
Unable to find a solution to do this within VB, we went about it this way:
The constructor takes two arguments, the dynamic object and the address of the event handler, and performs the C# method of adding handlers:
public TwainHandler(dynamic twainSession, Delegate eventHandler)
{
twainSession.AcquirePage += eventHandler;
}
The C# library was built and the dll added as a reference to the VB project.
Dim t as TwainHandler = New TwainHandler(TwainSession, New AcquirePageDelegate(AddressOf AcquirePageEventHandler))
This way the C# library adds the handler for the event (which points to a method in our VB application) dynamically. If anyone has a better solution please share.