Results 1 to 4 of 4

Thread: [RESOLVED] [2005] Can variables in private sub be used outside ??

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    63

    Resolved [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

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    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.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    63

    Re: [2005] Can variables in private sub be used outside ??

    Thx guys.. i got the point


    cheers
    Abhilash

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width