Scope, Variables

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
 
H

Herfried K. Wagner [MVP]

* "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.
 
H

Hal Rosser

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.
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top