7

I have a large VB6 projects where a lot of variables don't have an explicitly defined type, so they automaticly default to Variant type. Finding all those by hand is a massive task, so is there any way to automate this? In VB.Net it's possible to disable all automatic use of variants using 'Option Strict', but VB6 doesn't have that option.

Right now I added DefByte A-Z to every class, which makes the default type 'Byte' instead of 'Variant'. This let me catch a lot of undefined variables at run-time, as soon as they are assigned a value larger than 255. But it's still not fully fool-proof.

Is there a more reliable way to detect all undefined variables?

Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
Maestro
  • 9,046
  • 15
  • 83
  • 116
  • Can't you simply search for the word "Variant" in the entire solution ? – pikzen Jan 19 '12 at 13:03
  • 1
    @pikzen : in VB6 if you don't specify the type at all you get Variant. – MartW Jan 19 '12 at 13:12
  • 1
    Also, if you have a line like "Dim x, y, z AS Integer", only z will be an Integer. x and y will be Variant. This behaviour changed with VB.NET, fortunately. – MartW Jan 19 '12 at 13:13
  • 5
    Try `DefObj A-Z` instead for more spectacular compile-time and run-time errors. – wqw Jan 19 '12 at 21:35

4 Answers4

5

I used to use Aivosto's Project Analyzer to pick up things like this. There's a demo version which will give you a good idea what it can do.

MartW
  • 12,348
  • 3
  • 44
  • 68
4

Decorate your modules with Option Explicit.

This phrase should go at the top of each module you create. When done so, it will cause a compiler error when undeclared variables are encountered.

Option Explicit will not, however, prevent type-less variable declarations, such as

Dim i

The variable i will be declared as a variant, and no compiler error will be thrown even with Option Explicit defined.

qxn
  • 17,162
  • 3
  • 49
  • 72
2

I don't think there's a "foolproof" way to detect all undefined variables. However, the Option Explicit statement will require that all variables be declared in the module in which the statement appears, so the compiler will flag any instances where that is not the case. There is also an IDE option that automatically adds this statement to the start of any new module.

Bob Mc
  • 1,980
  • 1
  • 28
  • 38
  • If you have used Option Explicit in your code, then why are you asking this question? – Mark Bertenshaw Jan 20 '12 at 09:28
  • @MarkBertenshaw - `Option Explicit` checks for undeclared variables. It does not check for variables declared are variant which is what the OP requires – Matt Wilko Jan 20 '12 at 10:18
2

Use an programmer's text editor (I use UltraEdit) and do a mass search across you project source directories.

Start with searching for Variant (obviously), though you probably already did that.

Next use a regular expression type search for something along the lines of:

 *Dim [a-zA-Z][a-zA-Z0-9_]*\p

That should get the Dim x scenario without the trailing As DataType.

Use *Dim [a-zA-Z][a-zA-Z0-9_]*,.* to find the Dim a, b, c As Integer type of scenarios.

Use *Dim .*, [a-zA-Z][a-zA-Z0-9_]*,.* for odd ball scenarios like Dim a As Integer, b, c As Long

Repeat the above seaches with Private and Global instead of Dim and that should get just about everything.

tcarvin
  • 10,715
  • 3
  • 31
  • 52
  • 1
    'ReDim' should also be included, because VB doesnt require a 'Dim' first. And it doesn't work for arrays, function parameters, return-values, etc. So while it's a nice effort, it has some loopholes ;) – Maestro Jan 19 '12 at 23:15
  • I've only used Redim with arrays. I did think of the parameter issue, but to be honest I'm not fluent enough with regular expressions to bang out one for each. I'll leave that to someone more ambitious than I :) – tcarvin Jan 20 '12 at 02:51
  • Can't Public and Private also be used to declare a field? Need regular expressions for those too. – MarkJ Jan 21 '12 at 08:14
  • Field? Field? Oh, you must mean variable. Well what about Function returns too? Procedure parameters? Regex looks like a clever hack until you see how threadbare it can be. – Bob77 Jan 21 '12 at 19:11
  • Admittedly imperfect but I'd take the 80% you can get with a dozen well coded searches over nothing at all! – tcarvin Jan 22 '12 at 18:19
  • @markj 8 mentioned repeating for Private but yes I missed Public. I also missed Friend. – tcarvin Jan 22 '12 at 18:22