Export (0) Print
Expand All

Keywords as Element Names in Code

Any program element — such as a variable, class, or member — can have the same name as a restricted keyword. For example, you can create a variable named Loop. However, to refer to your version of it — which has the same name as the restricted Loop keyword — you must either qualify it by preceding it with its full namespace, or enclose it in square brackets ([ ]), as in the following examples:

MyForm.Loop.Visible = True   ' Qualified with the full namespace.
[Loop].Visible = True   ' Square brackets also work.

If you do not, Visual Basic assumes use of the intrinsic Loop keyword and produces an error, as in the following example:

Loop.Visible = True   ' Causes an error.

You can use square brackets when referring to forms and controls, and when declaring a variable or defining a procedure with the same name as a restricted keyword. It can be easy to forget to qualify names or include square brackets, and thus introduce errors into your code and make it harder to read. For this reason, it is recommended that you not use restricted keywords as the names of program elements. However, if a future version of Visual Basic defines a new keyword that conflicts with an existing form or control name, you can use this technique when updating your code to work with the new version.

Note   Your program also may include element names provided by other referenced assemblies. If these names conflict with restricted keywords, placing square brackets around them forces Visual Basic to accept them.

See Also

Visual Basic Naming Conventions | Program Structure and Code Conventions | Keywords

Show:
© 2014 Microsoft