[RESOLVED] [2005] Can variables in private sub be used outside ??
Hey guys!
I have a program in which i hav declared a few variables inside a 'Private Sub button_click'. I need to reuse the same variables in another Private Sub Button_Click.. Do i need to declare the same variables again in the second sub or can i continue to use it after i have i declared it in the previous sub.
Or do i declare the variables outside the subs and then use it in all the subs.
please advise
cheers
Abhilash
Re: [2005] Can variables in private sub be used outside ??
It depends on if you need same Variables (which means the same data) in multiple subs. You can declare them at a Module level and use them in the methods in that module.
Re: [2005] Can variables in private sub be used outside ??
Local variables are not accessible outside the block in which they are declared. If you declare a variable inside a method then outside that method that variable simply doesn't exist. You can declare another variable in another method with the same name but those two variables are not related in any way whatsoever. If you need a variable to be accessible from two independent blocks then the variable must be declared outside both of them. If those blocks are methods then the variable must be declared at the class-level or equivalent.
Note that by "block" I mean any section of code that starts at one line and ends at a corresponding End statement or the like, like Function...End Function, If...End If, For...Next, etc.
Re: [2005] Can variables in private sub be used outside ??
Thx guys.. i got the point
cheers
Abhilash