none
How to create a Global variable in VB 2008

Answers

  • If the variable used only in a sub, its call local variable for the sub.

    If you meant global variable that used across methods/functions/subs in a class, you may used like this

    public class myclass
    dim variable as string
    
    public function ReturnVariable() as String
       return variable
    end function
    
    public sub SetVariable(byval v as string)
        variable = v
    end sub
    
    end class
    

    • Proposed as answer by John Anthony Oliver Wednesday, January 18, 2012 3:22 AM
    • Marked as answer by packpatfan Wednesday, January 18, 2012 3:23 PM
    Wednesday, January 18, 2012 3:19 AM

All replies

  • you can't - a variable declared within a method has scope only within that method

    from MSDN:

    Procedure Scope

    An element declared within a procedure is not available outside that procedure. Only the procedure that contains the declaration can use it. Variables at this level are also known as local variables. You declare them with the Dim Statement (Visual Basic), with or without the Static (Visual Basic) keyword.

     

    you can create a class-level (global) variable outside of any methods, and give it a value within a method, however

    here are some links about scope:


    http://www.google.com/search?q=vb.net+scope+of+variables&rlz=1I7DKUS_en&ie=UTF-8&oe=UTF-8&sourceid=ie7


    • Edited by jwavila Tuesday, January 17, 2012 8:09 PM
    Tuesday, January 17, 2012 7:59 PM
  • Okay, Im a newbie to VB, so how do you create a Global variable inside a sub.


    Hello,

    it is impossible to declare a global variable in a method. But You can add a module to Your project and declare a friend variable here. You have access from anywhere in Your project to this.

    regards Ellen

     


    Ich benutze/ I'm using VB2008 & VB2010
    Tuesday, January 17, 2012 8:36 PM
  • To add to what's been said, ask yourself if you really have a need for a scope that large. There's more information about scope in general here.

    Typically, try to NOT scope something any further out than you need to.

    Tuesday, January 17, 2012 9:07 PM
  • To add to what Ellen has said,

    you can also add a public shared variable into a module so there is only one instance of it.

    Tuesday, January 17, 2012 9:19 PM
  • If the variable used only in a sub, its call local variable for the sub.

    If you meant global variable that used across methods/functions/subs in a class, you may used like this

    public class myclass
    dim variable as string
    
    public function ReturnVariable() as String
       return variable
    end function
    
    public sub SetVariable(byval v as string)
        variable = v
    end sub
    
    end class
    

    • Proposed as answer by John Anthony Oliver Wednesday, January 18, 2012 3:22 AM
    • Marked as answer by packpatfan Wednesday, January 18, 2012 3:23 PM
    Wednesday, January 18, 2012 3:19 AM
  • If the variable used only in a sub, its call local variable for the sub.

    If you meant global variable that used across methods/functions/subs in a class, you may used like this

     

    public class myclass
    dim variable as string
    
    public function ReturnVariable() as String
       return variable
    end function
    
    public sub SetVariable(byval v as string)
        variable = v
    end sub
    
    end class
    

    Thank you Ketchot! This is just what I needed! So I create a public class and declare a variable in it and it makes it available across all of my subs, functions, etc.?



    • Edited by packpatfan Wednesday, January 18, 2012 3:25 PM
    Wednesday, January 18, 2012 3:25 PM