Variable Scope, please help

  • Thread starter Thread starter al
  • Start date Start date
A

al

Hi,

How can I make a variable declared in a function to be global.That is,
to be seen all over the form code or class I tried to declare the
variable as public, but it didn't work.

MTIA,
Grawsha
 
al said:
How can I make a variable declared in a function to be global.

In a function is local, never global.
That is,
to be seen all over the form code or class I tried to declare the
variable as public,

That's what I would have suggested now.
but it didn't work.

"Didn't work" is no good description.
 
al said:
Hi,

How can I make a variable declared in a function to be global.That is,
to be seen all over the form code or class I tried to declare the
variable as public, but it didn't work.

MTIA,
Grawsha

Maybe I don't understand your question, but...

Class Foo

Private FooBar ...

Private Sub...
Dim Bar...
Private Sub1...

End Sub

Private Sub X...

End Sub

End Sub

Private Sub2...

End Sub

End Class

FooBar would be visible to all the Subs in the class Foo. The variable Bar
would be visible to the subs inside the sub in which it is declared.

Want it truly global? Declare it outside of your very first construct.

This answer seems too simple, so what did I miss about your question?
 
.. . .
How can I make a variable declared in a function to be global.

Can't be done, for two reasons.

1. Variables declared in a function are local (or possibly Static),
/never/ any more than that. If you want a variable with wider
scope, follow the language rules and declare it elsewhere.
2. "Global" variables can /only/ be declared in Modules, not Classes
(remember Forms /are/ Classes).

To make the variable accessible throughout a Class, make it an
instance variable of the Class, as in

Class abc123
Private siText as String = ""

' This variable can be seen anywhere in /this/ class
' but /not/ outside it
. . .
End Class

HTH,
Phill W.
 
Back
Top