Tuesday, November 20, 2012

Gotcha's when porting Visual Basic Code to Real Studio

The other day we got an interesting and initially puzzling bug report from a user who was porting a fairly large project from Visual Basic to Real Studio. Initially the error message didn't seem to make sense to me or Jason in Tech Support.

Our first step was to make up a simple example project. Once we had managed to trim things down to our example project and we could reproduce the error, it all made perfect sense. 

Our example started with a new desktop project, we put two pushbuttons on the default window: PushButton1 and PushButton2. In the action event of Pushbutton1 we put:


In PushButton2's action event we put:

         msgbox "hello world"

Run the project and you will get an error that says:

   This method requires more parameters than were passed

THis is odd since there's no method anywhere called "PushButton2_Action". However, in our example the error tells you precisely where to look:

Window1.PushButton1.Action, line 1, This method requires more parameters than were passed, PushButton2_Action

In Visual Basic you could call the "action" event of a push button just like any other method; and the name was very predictable. The action event was named:


And it may or may not have had parameters that got passed to it. And if you grab a Visual Basic project and port it Real Studio you may still have this kind of code left over.

So how do you find this kind of left over code in ported VB projects? 

I have to admit that in a big project that is going to be a big job. If you know the old code base, you might hunt for control names followed by a _ . So in our sample you might look for "PushButton2_" and then deal with all the items you find since this syntax is unlikely to be valid in Real Studio. Or you might hunt for the "_Action" or other names that were used in VB to call other controls event handlers.

Now that you've found it, what can you do to fix it?

The easiest thing to do is to move the code in the action event handler that was being invoked into a method that can be called from where ever needed. Can you call the event directly? Let's just say that might work but it's unsupported - so don't do it.

In the end, the compiler correctly identified the error but the error message was easy to misunderstand. So when you DO get an error have a VERY close look.

Oh, and leave the VB habits behind :)

1 comment:

Bob Keeney said...

This is part of the reason why we, as a company, don't 'port' VB projects. We rewrite them.

As I tell our consulting clients all the time, while Visual Basic and REALbasic might have 'basic' in the name, the two are completely different languages separated by almost 20 years. There are things that take pages of code in VB that take 3 or 4 lines of code in Real Studio. No automated tool can ever get it right.

We've done dozens of VB6 rewrites. At the end of the day you'll be better off rewriting rather than trying to convert your VB6 apps to Real Studio.