Results 1 to 13 of 13

Thread: Universal Variable

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    400

    Universal Variable

    Can I create a variable in VB that I can access in any form or module? I just want to know if that's possible and how to do it. I don't want any alternatives. Thank you

  2. #2
    New Member
    Join Date
    Aug 2005
    Posts
    15

    Re: Universal Variable

    I know one way is to declare a Global variable outside of a function or a sub.
    I think this can only be done inside a module, but I could be wrong on this

    Code:
    Global MyVar as String
    This will then work across the entire project.

    EDIT - Make sure that you declare this at the very beginning of a module, ie, before you have begun a Sub or Function.

  3. #3
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: Universal Variable

    I think it's usual to use the Public keyword nowadays rather than the Global keyword.
    You can use a Public variable in a form but you have to specify the form name when you wish to reference it outside of the form... Form1.MyPublicString. I believe this is not recommended because a Form unlike a Module is effectively a Class which can be unloaded and dereferenced.

  4. #4
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Universal Variable

    Quote Originally Posted by Milk
    You can use a Public variable in a form but you have to specify the form name when you wish to reference it outside of the form... Form1.MyPublicString. I believe this is not recommended because a Form unlike a Module is effectively a Class which can be unloaded and dereferenced.
    When talking about a public variable inside a form, you're really talking about adding a public property to a form:
    vb Code:
    1. Option Explicit
    2.  
    3. Private mstrTest As String
    4.  
    5. Private Sub Form_Load()
    6.     mstrTest = "Hello, World!"
    7. End Sub
    8.  
    9. Public Property Let Test(pstrTest As String)
    10.     mstrTest = pstrTest
    11. End Sub
    12.  
    13. Public Property Get Test() As String
    14.     Test = mstrTest
    15. End Sub
    Debug.Print Form1.Test

  5. #5
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: Universal Variable

    Creating as a property is a better idea, as it will be a property anyway. So by declaring Public Test As String you're actually doing the same Ellis Dee posted above, just with a shorter syntax, but if you want to have any handling you have to do the long syntax anyway.

    As for creating public properties such as that one, they're useful in some cases even in forms. Say you have made a form of which there can be multiple copies running, but you want to control certain aspect individually, such as you've added a gradient background feature and thus want to add properties BackColor2 (OLE_COLOR) and Gradient (Boolean) to have control over the new feature.

    However, keeping public information such as program settings is better to do in a module or centralize it into it's own class module. This goes as far as into the application design: forms as visual and interactive elements should be dedicated for that, and shoudn't be used as the main storage of the information.

  6. #6
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Universal Variable

    On top of any module (that is not form or class), declare:
    Code:
    Public myUniversalVariable As AnyType
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  7. #7
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Universal Variable

    Public has replaced Global
    Private has replaced Dim

    All work but Public and Private are now perfered over Global and Dim

  8. #8
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Universal Variable

    Quote Originally Posted by jmsrickland
    Public has replaced Global
    Private has replaced Dim

    All work but Public and Private are now perfered over Global and Dim
    Public has replaced Global: OK!
    Private has replaced Dim: Not quite right!

    You cannot replace
    Code:
    Sub ABC()
       Dim x As Long
       ...
    End Sub
    with
    Code:
    Sub ABC()
       Private x As Long
       ...
    End Sub
    Private can only be used instead of Dim at Module level.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  9. #9
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    Re: Universal Variable

    Well, that's what we were talking about; a universal variable as the OP put it.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2005
    Posts
    400

    Re: Universal Variable

    Okay, thank you! So, what's the difference between using "Global" or "Public" in a module? Also, I was wondering, is "Dim" short for something?

  11. #11
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Universal Variable

    Along with "Public has replaced Global", the statement "Private has replaced Dim" is misleading.

    "Public" is the new keyword for "Global" (both can be used interchangeable), they can be used to declare a variable on top of a module or a procedure (Sub/Function), these variable and procedure can be accessed anywhere in the project (unless there is other restriction).

    "Private" and "Dim" can be used interchangeable at Module level only, and "Private has not replaced Dim".
    "Private" is also can be used to declare a procedure that will be available within the module only.
    Last edited by anhn; Jun 18th, 2008 at 07:54 PM.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  12. #12
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: Universal Variable

    "Dim" is short for "Dimension"

  13. #13
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Universal Variable

    "Dim" is "Dim", it means dimension but it is not the short form of "Dimension", ie. "Dimension" cannot be used in places of "Dim".
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

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