Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using events declared in Visual Basic 6.0 in a dotnet application

Tags:

.net

vb6

we are writing tests for a COM library written in VB 6.0.The problem we are facing is that, we are unable to access events declared in VB( withevents). We get exception, "object does not support set of events". How can we overcome this problem?


1 Answers

Your mocking framework is the problem here. The mock object returned by this call:

repository.DynamicMock<PersonLib.DatabaseCommand>();

implements the DatabaseCommand class's interface, but does not mock its events. Therefore, when you pass an instance of this mock object to your VB6 code, which expects to receive a DatabaseCommand object that can raise events, it won't work.

When you pass the mock object to your PersonClass.Init method, here is simplified version of what is happening:

  1. The code gets to this line in PersonClass.Init:

    Set dbCommand = vDBCommand

  2. VB6 asks the object on the right-hand side of the Set statement if it supports the same events that the DatabaseCommand class does (VB6 does this because you declared dbCommand with the WithEvents keyword, so it will try to set up an event sink to receive events from the dbCommand object).

  3. The object you passed in, however, being a mock object and not a real DatabaseCommand object, doesn't actually implement the events that the real DatabaseCommand class implements. When VB6 encounters this, it raises the error you are seeing.

I can't think of a way to make the mock object support the same events that the DatabaseCommand class does in order make your test code work (well, I can think of one way, but it would involve redesigning your classes), but I may post more later if I find a more reasonable solution.

like image 152
7 revsMike Spross Avatar answered Sep 07 '25 21:09

7 revsMike Spross