Scope, Variables

  • Thread starter Thread starter Dennis D.
  • Start date Start date
D

Dennis D.

Visual Studio Code Editor
I get a '...not declared' syntax error after the initial variable
declaration in any other subs or functions. Dim has module level scope so
any subs or functions in the class should recognize iAnything. Why the
syntax errors?
For example:

Class aclass...
Private Sub blabla
dim iAnything as Integer
iAnything = 5 'ok
End Sub

Private Sub AnotherSub
Dim oSomething as Integer
oSomething = iAnything 'syntax error - iAnything not declared
End Sub

Function Afn(ByVal...)As Atype
Dim oSomething as Integer
oSomething = iAnything 'syntax error - iAnything not declared
End Function
End Class
 
* "Dennis D. said:
Visual Studio Code Editor
I get a '...not declared' syntax error after the initial variable
declaration in any other subs or functions. Dim has module level scope so
any subs or functions in the class should recognize iAnything. Why the
syntax errors?

Declare 'iAnything' /outside/ a method/property.
 
remember - wherever you declare the variable is where it lives
declare it in a sub - and you cannot use it outside the sub - except for
passing as args.
 
I suppose you also know that any variable declared inside a For...Next loop
or If...end if block only has scope within the blocks in which it was
declared.
 
Back
Top