Got a VB6 legacy project. Now what? Part 1Being assigned a legacy VB6 project without knowing the language is a difficult task. In this series (I plan to have at least one more installment about it), I will share what is my approach in dealing with an unfamiliar VB6 codebase.
The first thing I do is open a file - be it a form, a module, or a class module - in Visual Studio and scan it to check if it has an Option Explicit statement.
If it has not, then I'd write it as its very first line.
Then, I hit Ctrl+F5 and see what happens. You should do the same. If you get an error message saying "Compile error: Variable not defined", go to the procedure where the unknown variable is being highlighted and declare it.
Variable declaration usually takes place right bellow the Sub, Function, or Property declaration, like this:
Public Property Let Value(V As Variant)
Dim VT As Integer
You see, by default, VB6 does not force you to declare your variables. It is a good thing when you are in that exploratory phase building your software. But once that phase is over, it is better to have mandatory variable declaration. It avoids a common type of mistake: Automatic creation of variables with typos.
Suppose you have a declared variable DefinitelyGood, but later on, you misspell it as DefinatelyGood. Without Option Explicit, VB6 will not complain about it, and it may remain unnoticed to you.
If you find a misspelled variable, you should use your best judgment about fixing the typo or declaring the misspelled variable. If the procedure has a logical error due to the misspelled variable, it is a no-brain fix. But if it is working, then you should consider declaring the misspelled variable, and maybe even changing its name to something different to avoid confusion.
When declaring a variable, if you can quickly figure out its data type, then declare it with that specific data type. But if you are unsure, you should declare it as Variant - it is the safer bet in this situation. Once you understand a little better the code base, you should be able to return there and narrow down that variable data type.
Sometimes, there are many, many variables not declared in some procedures. I'm talking about dozens of them. You should strive to have them all declared, but this is not an inflexible rule.
Sometimes it is just unpractical to do that. In these cases, you should comment the Option Explicit statement and maybe return to this file later.
Don't fall into thinking that all variables in the example below are Dates:
Dim A, B, C As Date
One VB6 quirk is that all variables should have its data type declared one by one. If the data type is missing, then it is implicitly Variant 1. The declaration above is the same as:
Dim A As Variant, B As Variant, C As Date
Once all files have been checked, you should go to the menu option Tools → Options → Editor tab, Code Settings section, and check the Require Variable Declaration setting if it is not already checked.
It will ensure every new file will have an Option Explicit statement by default.
Then you can revisit every procedure - or the most important ones - and, if there are not many variable declarations there, comment all of them and Ctrl+F5 the project. Then uncomment every needed variable and Ctrl+F5 it again, until there are no more compile errors. Doing that you will be able to spot declared variables that are not being used. Deleting them decreases the mental burden of tracking many variables for that particular procedure.
Also, when trying to wrap your head around some procedures, you will be able to spot global variables. They are the ones being used but not declared in that procedure.
VB6 legacy projects usually have modules declaring several global variables. Commenting them, testing with Ctrl+F5, and uncommenting them as needed as mentioned above will help you get rid of some of them too. Sometimes you can even identify some variables that should not be global at all. They are there in the module but are being used only in one another file. In these cases, it is better to move them to the declaration section 2 of the file where it is being used. Decreasing the number of global variables is always a good thing.
Next in my chest of tricks is going after variable declarations in the declaration section that does not have Private or Public keywords; the ones using only Dim.
I change Dim to Private and test it with Ctrl+F5. If there's a compile error, then I change it to Public.
If the variable there is not being used by other parts of the project, it has no business being declared implicitly Public with Dim. Narrowing variables' scopes is the right thing to do.
If you follow these guidelines, you're up to a good start.
Second part is available here.
1 It depends whether the file has a Deftype or not. It's unusual to find one being used, but it is not impossible, either.
2 The region before any procedure declaration is called the declaration section. It is there that Option Explicit, Option Base, and Option Compare have place, as well as global or file scoped variables are declared.