Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2004

    Unanswered: How to declare a global variable in VB?

    How to declare a global variable in VB?

  2. #2
    Join Date
    Oct 2003
    A global variable is one that is declared outside of any sub or function, i.e. in the prologue section of the module.

    Whereas most languages would assume that a variable so declared is "private (to the module) unless explicitly declared global," Visual Basic makes exactly the opposite assumption and provides a private keyword to declare otherwise. (Yet... variables use public! Go figure...)

    The "rather loosy-goosy" (imho) handling of variables in the Visual Basic language can be (again, imho) quite problematic. The declaration option explicit is, yet again imho, quite essential. Otherwise, "tpyos" simply become new variables, not typos!
    Last edited by sundialsvcs; 01-11-04 at 19:53.
    ChimneySweep(R): fast, automatic
    table repair at a click of the

  3. #3
    Join Date
    Feb 2002
    Create a module (usually referred to as a standard code module) and under the "General" "Declarations" insert something like:

    Public gstrConnectionString As String

    Make sure to have a naming convention that is documented. Also, make sure that globals are necessary - they are nasty creatures.

    Option explicit makes variable declarations mandatory. Without it, you could type in a variable name that was not exactly like the one you declared and vb would make that "new" variable a variant. If you follow any type of naming convention, you will have a mix of upper/lower case characters in your declarations - such as iCounter (mean an integer that will be used as a counter). So I always make sure that programmers who are using vb, make sure that when referencing a declared variable to type it in all lowercase - because when you hit enter, if vb finds a match it will automatically convert that name to its declared name (mix of upper/lower characters). If you do not see that happen, then you know it is a typo or was never declared.

  4. #4
    Join Date
    Jan 2004
    Got it! Thank you very much!

Posting Permissions

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