Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Priority of private/public variables with same name

Status
Not open for further replies.

cyberbiker

Programmer
Mar 16, 2001
431
US
First off, I did not do this. I may be goofy, but....

We have a ridiculous problem in an existing program.

We have a public variable declared in a module as database
In numerous places throughout the application, a module level variable is declared with the same name (also as database)
As far as I can tell at any given time the program is looking for the same ACCESS data base either way, but this app has had a history of "strange" problems. I have no way to be certain of what error messages have been generated over time.

My basic question is whether the module level variable overides the public or vice versa if any one knows.
It is almost a moot point, since I am working on fixing this now (along with other changes), but an answer might help me understand a bit better what has been going on.

The app gets the full path to the ACCESS database fom an *.ini file regardless.
Would having 2 defined variables be creating 2 connections to the ACCESS database?

My change will be to using MSDE and SQL server 7 tables through an ODBC connection so I will be changing quite a bit anyway.
Before anybody suggests being sensible and using ADO, that is not an option Terry (cyberbiker)
 
When a variable is referenced:

A module variable will be used in preference to a global variable of the same name.
A local procedure variable will be used in preference to any other variable of the same name.

Glyn.
 
Terry,

The variable with the smallest scope has precidence. The order is like this from highest to lowest

LOCAL (Dimmed inside a Sub or Function)
Module (Dimmed inside a Form or Module with a Private statement)
Global (Dimmed inside a Module with a Public statement)

Following test shows this is true:

Create a new project and place this code in the form:

Code:
Option Explicit

Private x As Integer

Private Sub Form_Load()
    Dim x As Integer
    
    x = 35
    
    ShowVar
    ShowPubVar
End Sub

Private Sub ShowVar()
    Debug.Print x
End Sub

Then create a Module and place this code in the module:

Code:
Option Explicit

Public x As Integer

Public Sub ShowPubVar()
    Debug.Print x
End Sub

When you step through the code, the local X is the one that gets assigned. If you comment out the local declaration, then you'll see that the private form level variable x in the form gets assigned. If you comment this form level variable out, you'll see the global one in the module gets assigned.

Cheers,
Snaggs
tribesaddict@swbell.net
Life can only be understood backwards; but it must be lived forwards.
 
Thanks a lot. That was my understanding, but I was not certain.
We have a pretty confusing mess here with this app. As I make the other changes, I will take care of this public/private variable thing, but that does not appear to have much to do with the underlying problems. A very important thing to know. Terry (cyberbiker)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top