Skip to content

Xojo Code Editor: Auto-complete and Scope

The Xojo Code Editor has a wonderful auto-complete feature that makes typing your code and discovering variables, methods and properties much easier. To activate auto-complete press the tab key after you have started typing some code.

When dealing with how variables are shown by auto-complete, you first need to understand the concept of a variable’s ‘visibility’ or scope.

When a variable is declared, its scope is limited to the block of code in which it is declared. For example, add a method with this code:

Dim myVariable As Integer
For myVariable = 0 To 9
  // Do something
  MsgBox(myVariable.ToText)
Next

In the above code the scope of the variable myVariable is the entire method. This includes being within the ‘For…Next’ loop.

Now consider this code in a method:

For myVariable As Integer = 0 To 9
  // Do something
  MsgBox(myVariable.ToText)
Next
// myVariable cannot be used here and will not auto-complete

The block of code within which myVariable is declared is the ‘For…Next’ loop.  The scope starts at ‘For’ and ends at ‘Next’. Because of this limiting scope myVariable does not auto-complete outside the loop and if you type it manually in code like this you will get a compile error for that line that says “This item does not exist”:

For myVariable As Integer = 0 To 9
  // Do something
  MsgBox(myVariable.ToText)
Next
myVariable = 10 // compile error here

(Thanks to Robin Lauryssen-Mitchell for this tip.)