4

How can I make the VB6 compiler fail when I forget to declare a variable?

This would stop various typing errors (both keyboard and data types) and errors like this when it tries to access something unexpected.

Problems caused by not correctly declaring variables:

Community
  • 1
  • 1
Deanna
  • 23,876
  • 7
  • 71
  • 156
  • dont forget to run using ctrl-F5 ... using just F5 doesnt compile, so no error message will show up until the problem is encountered during runtime. – Hrqls Apr 09 '13 at 11:46

1 Answers1

8

You should use Option Explicit. This should be put on the first line of every module and form's code section.

You can also configure the VB6 IDE to add this automatically to all new modules by going to Tools > Options > Require Variable Declaration.

Deanna
  • 23,876
  • 7
  • 71
  • 156
  • 2
    +1. [Also](http://stackoverflow.com/questions/664370/your-favorite-visual-basic-6-0-tools-and-tips) uncheck the "Tools-options-general-compile on demand" option so that you are told about errors immediately. And consider [using](http://stackoverflow.com/questions/1904884/are-deftype-statments-considered-bad-practice) DefObj A-Z to detect declarations with no explicit type e.g. Dim x instead of Dim x As String – MarkJ Apr 10 '13 at 17:20