Wednesday, December 12, 2012

What is explicit and implicit declaration?


Following the traditions of earlier versions of Basic, you don’t actually need to declare a variable at all to use it just using it in code declares it as a variant if it’ s not been declared. It’s better to require all variables to be explicitly declared, however, because misspelling a variable name can declare a new variable and cause problems.

A nice feature of Visual Basic is its ability to warn you or prevent you from typing a variable name without explicitly declaring it as a variable. You should always use this built-in safety feature. To implement it, simply type the statement Option Explicit in the first line of a form’s code window (outside any procedures). You only need one Option Explicit statement for any Visual Basic project. You can also turn on Option Explicit for your standard EXE projects by clicking on the Tools menu, choosing Options, clicking the Editor tab, and selecting Require Variable Declaration.