+ Reply to Thread
Results 1 to 3 of 3

Global/Public

  1. #1
    Fox via OfficeKB.com
    Guest

    Global/Public

    If I have any global variable and/or constants to declare, and they are
    limited to one module, which is the better method to declare them:

    Option Explicit
    Dim myString as String
    Const myConst as Integer = 1

    Or

    Option Explicit

    Sub Main()
    Public myString as String
    Public Const myConst as Integer = 1

    What are the advantages and/or limitations of each? And while we're at it,
    what about declaring as variable as Static?

    --
    Message posted via OfficeKB.com
    http://www.officekb.com/Uwe/Forums.a...mming/200605/1

  2. #2
    Bob Phillips
    Guest

    Re: Global/Public

    The limitations of the latter is that it doesn't work.

    If they are public to the whole project, accessible from any module, declare
    them as public in declaratives

    Public myString as String
    Public Const myConst as Integer = 1

    If they are private to the module, accessible only to that module, declares
    them as private in declaratives

    Private myString as String
    Const myConst as Integer = 1

    If they are private to the procedure, not accessible b y any other
    procedure, in that module or any other, declare them as private in the
    procedure

    Sub Main()
    Dim myString as String
    Const myConst as Integer = 1

    --
    HTH

    Bob Phillips

    (replace somewhere in email address with googlemail if mailing direct)

    "Fox via OfficeKB.com" <u18899@uwe> wrote in message
    news:60d655f7da51d@uwe...
    > If I have any global variable and/or constants to declare, and they are
    > limited to one module, which is the better method to declare them:
    >
    > Option Explicit
    > Dim myString as String
    > Const myConst as Integer = 1
    >
    > Or
    >
    > Option Explicit
    >
    > Sub Main()
    > Public myString as String
    > Public Const myConst as Integer = 1
    >
    > What are the advantages and/or limitations of each? And while we're at

    it,
    > what about declaring as variable as Static?
    >
    > --
    > Message posted via OfficeKB.com
    > http://www.officekb.com/Uwe/Forums.a...mming/200605/1




  3. #3
    Jim Thomlinson
    Guest

    RE: Global/Public

    This is getting into the realm of scope. What should have access to what. In
    general the rule is to keep things as private as is possible. It makes
    debugging a whole lot easier. For example if your variable is private to the
    current module then if there is a problem with the value of that variable at
    any point you can at least narrow it down immediately to the current module
    and rule out the rest of the project as the cause of the problem. This might
    sound simple but when your projects start to get very large it is immensly
    helpful. That is just one reason. There are more but for the sake of
    simplicity "as much as possible keep things as private as you can". Your life
    will be better for it...

    Static Variables - Under normal circumstances when a procedure or function
    ends all of the variables are destroyed and their value is lost. Normally
    that is what you want, but in the odd case you would like to keep that value.
    Your options are to declare a global varaible (which is overkill if this is
    the only procedure that needs access to this variable. A scope issue again.)
    or to declare the variable as static. Static variables persist even after the
    procedure ends, so that then next time you call the procedure the value is
    still there.
    --
    HTH...

    Jim Thomlinson


    "Fox via OfficeKB.com" wrote:

    > If I have any global variable and/or constants to declare, and they are
    > limited to one module, which is the better method to declare them:
    >
    > Option Explicit
    > Dim myString as String
    > Const myConst as Integer = 1
    >
    > Or
    >
    > Option Explicit
    >
    > Sub Main()
    > Public myString as String
    > Public Const myConst as Integer = 1
    >
    > What are the advantages and/or limitations of each? And while we're at it,
    > what about declaring as variable as Static?
    >
    > --
    > Message posted via OfficeKB.com
    > http://www.officekb.com/Uwe/Forums.a...mming/200605/1
    >


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1