Consulting

Results 1 to 5 of 5

Thread: Variables and scope

  1. #1
    VBAX Regular
    Joined
    Jul 2014
    Posts
    79
    Location

    Variables and scope

    Guys,

    The number of variables I have in my project is beginning to mount and I am still not completely satisfied with how I am using them with regards to being local/public. I am running into some problems and I feel my variables are scattered everywhere.

    This is my current thinking although I am starting to get a bit confused:

    Any variables which are used between modules, I have defined as Public in the module which calls my userform.
    Any variables which are used throughout a module have been defined in the general declarations area of that module using Dim (which in this case is identical to Private?).
    Any variables which are needed in a procedure only have been defined within that procedure using Dim.

    So if I am entering data into an array which has to be available to all modules and procedures for the duration of the running of the userform, I have declared them as "Public" in the module which calls my userform.

    So if I am using counters i, j, x, y etc. in multiple modules, should I be declaring them in each procedure? or privately for the module to save re-dimensioning them each time?

    My thought was that they should be re-dimensioned each time to ensure they are being destroyed when not needed.

    Anyway, my job today will be to go through my code and try to organise all my variables correctly. Any tips / help on the best way to do this would be a great help.

    Cheers guys.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    If I have a lot of globally scoped variables, I tend to have a specific module just to hold them as well as any global Const's -- I just find it easy to housekeep them that way

    Any counters that are used in a procedure, I Dim inside the procedure

    Personal Style
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    I do not recommend using global variables to pass information between one procedure and the next. That is what Procedure Parameters are for. It is too easy to introduce very hard to find bugs when using variables to pass data.

    Global Constants are an entirely different subject. They will help prevent typos and make it very easy to maintain your code base when, for example, company information changes.

    Const CompPublicPhNum As String = "(800) 555-1212"
    Const CompPresidentName As String = "Superman"
    Const CompAddress As String = "1 Broadway, Ste 1"
    'Etc
    In fact, all business rules should be kept as constants in one location.

    My general rule is that it is far better to type "Dim Something As String" 100 times, than to have it left with a value from some other sub even once a year.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    I do not recommend using global variables to pass information between one procedure and the next. That is what Procedure Parameters are for. It is too easy to introduce very hard to find bugs when using variables to pass data.
    /
    Agree that the use of Globals should be avoided as much as possible (esp if you can use a passed parameter), but sometimes you just gotta use 'em
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Yep, Sometimes I just can't figure out a way to avoid them. But, I try really hard.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

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