Results 1 to 1 of 1

Thread: Classic VB - What is the difference between Dim/Private/Public/Global/Static/Const?

  1. #1

    Thread Starter
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Classic VB - What is the difference between Dim/Private/Public/Global/Static/Const?

    Where (and how) a variable/constant is declared affects where it can be used - what is known as the Scope of the variable.

    When talking about Scope we tend to use 3 names for it: Local (only available within a certain procedure), Private (can be used within a whole form/module/..), or Public (can be used in the entire project).

    Typically, a variable will be declared like this: Dim MyVar as Integer
    ..but you can use other keywords instead of Dim, and they all have different effects on the Scope. Below is an explanation of each of the keywords you can use.

    Declarations within a procedure (sub/function/..)
    By declaring a variable within a procedure, it can only be used in that procedure - there is no way to use it anywhere else in your program.

    There are three keywords you can use:
    Dim
    Each time the procedure is called, the value is reset to the default value for the data type of the variable (0 for numbers, "" for normal strings [fixed length strings are filled with Chr(0)], False for booleans, ...).

    Static
    The variable keeps the previous value each time you call the procedure.

    Const
    The 'variable' (actually Constant) has a fixed value, which is specified on the declaration line (eg: Const MyNum = 3 )

    Declarations in the General Declarations section at the top of a code file (includes forms/modules/classes/...)
    By declaring a variable in the General Declarations section, it can be used by every procedure in that code file - and sometimes by other code modules too.

    The variable keeps its value for as long as the code file is loaded (note that you cannot unload a standard module [.bas], so variables in a standard module will keep their value for the life of the program).

    There are five keywords you can use:
    Private
    The variable can only be used in the code file which it is declared in.

    Public
    The variable can be used by any code file in the project. Note that if it is declared in a form/class (say Form1), then you also need to specify the form/class when using the variable in other code files (eg: Form1.MyVariable = 2)

    Dim
    This works in the same way as Private - which ideally you should be using instead, to make it clear.

    Const (should be either "Private Const" or "Public Const")
    As in a procedure, the 'variable' (actually Constant) has a fixed value. You should also use the Public or Private keyword to specify what scope you want it to have (by default, it will be Private).

    Global
    This is similar to Public, but is only allowed in standard modules. This is the "old" version of Public, and should not really be used any more.


    Some extra things to think about
    If you have two (or more) variables with the same name but different Scope, the most "local" Scope applies first.. for example, if you declare MyVar in a procedure and in the General Declarations section of the same code file, any code in that procedure will work with the version of MyVar that is declared in the procedure, not with the one in the General Declarations section.

    If you haven't actually declared the variable ( ), what is the scope of it? If you don't know, your program may not work how you want it to! This isn't the only reason that declaring your variables is a good idea - it also reduces 'random' errors, as can be seen in this FAQ article (Option Explicit).
    Last edited by si_the_geek; Feb 14th, 2009 at 09:24 AM. Reason: altered wording for clarity

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