 |
|
|
|
| Author |
Message |
Andrew Chalk
Joined: 04 Oct 2007 Posts: 15
|
Posted: Sun Jul 23, 2006 7:23 pm Post subject: VB6 Scope Issues |
|
|
1) What is the difference in scope of:
Public X As String
Global X As String
2) What is the difference in scope of:
Private Sub S()
Sub S()
In (2): Nothing, I believe. The absence of a scope qualifier on a Sub or
Function implies Private, right?
Many thanks.
Archived from group: microsoft>public>vb>syntax |
|
| Back to top |
|
 |
Bob O`Bob
Joined: 04 Oct 2007 Posts: 1456
|
Posted: Sun Jul 23, 2006 5:50 pm Post subject: Re: VB6 Scope Issues |
|
|
Andrew Chalk wrote:
> 1) What is the difference in scope of:
> Public X As String
> Global X As String
Anywhere where the latter is not a syntax error, there is no difference between the two.
Anywhere else (place where you can only use Public) it /is/ actually rather different
from *either* of the above.
>
> 2) What is the difference in scope of:
> Private Sub S()
> Sub S()
>
> In (2): Nothing, I believe. The absence of a scope qualifier on a Sub or
> Function implies Private, right?
I think that's the case, but it's not something I'm likely to ever bother to test.
VB has, over the years, given me a serious distaste for defaults.
Bob
-- |
|
| Back to top |
|
 |
Andrew Chalk
Joined: 04 Oct 2007 Posts: 15
|
Posted: Sun Jul 23, 2006 8:01 pm Post subject: Re: VB6 Scope Issues |
|
|
"Bob O`Bob" wrote in message @TK2MSFTNGP03.phx.gbl...
> Andrew Chalk wrote:
>> 1) What is the difference in scope of:
>> Public X As String
>> Global X As String
>
>
> Anywhere where the latter is not a syntax error, there is no difference
> between the two.
>
> Anywhere else (place where you can only use Public) it /is/ actually
> rather different
> from *either* of the above.
>
Can you explain the last sentence? It is not clear what the "it" refers to.
Thanks. |
|
| Back to top |
|
 |
Andrew Chalk
Joined: 04 Oct 2007 Posts: 15
|
Posted: Sun Jul 23, 2006 8:02 pm Post subject: Re: VB6 Scope Issues |
|
|
"Bob O`Bob" wrote in message @TK2MSFTNGP03.phx.gbl...
>> 2) What is the difference in scope of:
>> Private Sub S()
>> Sub S()
>>
>> In (2): Nothing, I believe. The absence of a scope qualifier on a Sub or
>> Function implies Private, right?
>
> I think that's the case, but it's not something I'm likely to ever bother
> to test.
> VB has, over the years, given me a serious distaste for defaults.
>
I agree. As a newbie to VB I wanted to explicitly write "Private" if that
was the default.
Thanks,
Andrew |
|
| Back to top |
|
 |
Bob O`Bob
Joined: 04 Oct 2007 Posts: 1456
|
Posted: Sun Jul 23, 2006 7:02 pm Post subject: Re: VB6 Scope Issues |
|
|
Andrew Chalk wrote:
> "Bob O`Bob" wrote in message
> @TK2MSFTNGP03.phx.gbl...
>> Andrew Chalk wrote:
>>> 1) What is the difference in scope of:
>>> Public X As String
>>> Global X As String
>>
>> Anywhere where the latter is not a syntax error, there is no difference
>> between the two.
>>
>> Anywhere else (place where you can only use Public) it /is/ actually
>> rather different
>> from *either* of the above.
>>
> Can you explain the last sentence? It is not clear what the "it" refers to.
In a standard or "code" module (file extension "bas") the two lines are
precisely equivalent, and either one actually instantiates the variable.
In a class or form module the Global declaration is illegal and the Public
declaration creates a property which requires a reference to an instance
before it actually means anything at all.
.... which is why I've established a /personal/ standard to NEVER use Public
where Global is legal. That way at least in /my/ code each keyword
[pretty much] always means the same thing. Some folks have argued that
the Global keyword is "deprecated" and should be replaced by Public,
but I most emphatically disagree.
Bob
-- |
|
| Back to top |
|
 |
argusy
Joined: 04 Oct 2007 Posts: 145
|
Posted: Mon Jul 24, 2006 6:50 pm Post subject: Re: VB6 Scope Issues |
|
|
Hi, Bob
I agree wholeheartedly with your last paragraph. If I know a variable is going
to be used throughout a program, and it is NOT specific to a module operation,
I'll declare globally.
For example, a boolean variable to flag a condition
public function
flag = false 'declared globally
do
if aCondition is true then flag = true
loop
if flag = false then doSomeOtherThing 'weak I know, could use it above
exit sub
I **could** have declared 'flag' as a local boolean variable
(dim flag as boolean)
and it disappears from scope when the function has ended.
or as a form variable
(public flag as boolean)
and then it disappears when the form is unloaded
PROVIDED I declare its condition before I use it, I don't see any problem
(If I didn't, and it has been set elsewhere, it could stuff up the logic within
this function - which is a good reason to declare it inside the function, isn't
it?. Hmmm. Bad choice, off the top of my head, but it serves the purpose for
Andrew's query)
Argusy
Bob O`Bob wrote:
> Andrew Chalk wrote:
>
>> "Bob O`Bob" wrote in message
>> @TK2MSFTNGP03.phx.gbl...
>>
>>> Andrew Chalk wrote:
>>>
>>>> 1) What is the difference in scope of:
>>>> Public X As String
>>>> Global X As String
>>>
>>>
>>> Anywhere where the latter is not a syntax error, there is no
>>> difference between the two.
>>>
>>> Anywhere else (place where you can only use Public) it /is/ actually
>>> rather different
>>> from *either* of the above.
>>>
>> Can you explain the last sentence? It is not clear what the "it"
>> refers to.
>
>
> In a standard or "code" module (file extension "bas") the two lines are
> precisely equivalent, and either one actually instantiates the variable.
>
> In a class or form module the Global declaration is illegal and the Public
> declaration creates a property which requires a reference to an instance
> before it actually means anything at all.
>
>
> ... which is why I've established a /personal/ standard to NEVER use Public
> where Global is legal. That way at least in /my/ code each keyword
> [pretty much] always means the same thing. Some folks have argued that
> the Global keyword is "deprecated" and should be replaced by Public,
> but I most emphatically disagree.
>
>
>
>
> Bob |
|
| Back to top |
|
 |
Andrew Chalk
Joined: 04 Oct 2007 Posts: 15
|
Posted: Mon Jul 24, 2006 6:42 pm Post subject: Re: VB6 Scope Issues |
|
|
Got it.
Thanks!
"Bob O`Bob" wrote in message @TK2MSFTNGP02.phx.gbl...
> Andrew Chalk wrote:
>> "Bob O`Bob" wrote in message
>> @TK2MSFTNGP03.phx.gbl...
>>> Andrew Chalk wrote:
>>>> 1) What is the difference in scope of:
>>>> Public X As String
>>>> Global X As String
>>>
>>> Anywhere where the latter is not a syntax error, there is no difference
>>> between the two.
>>>
>>> Anywhere else (place where you can only use Public) it /is/ actually
>>> rather different
>>> from *either* of the above.
>>>
>> Can you explain the last sentence? It is not clear what the "it" refers
>> to.
>
> In a standard or "code" module (file extension "bas") the two lines are
> precisely equivalent, and either one actually instantiates the variable.
>
> In a class or form module the Global declaration is illegal and the Public
> declaration creates a property which requires a reference to an instance
> before it actually means anything at all.
>
>
> ... which is why I've established a /personal/ standard to NEVER use
> Public
> where Global is legal. That way at least in /my/ code each keyword
> [pretty much] always means the same thing. Some folks have argued that
> the Global keyword is "deprecated" and should be replaced by Public,
> but I most emphatically disagree.
>
>
>
>
> Bob
> -- |
|
| Back to top |
|
 |
Karl E. Peterson
Joined: 04 Oct 2007 Posts: 4836
|
Posted: Mon Jul 24, 2006 6:20 pm Post subject: Re: VB6 Scope Issues |
|
|
Andrew Chalk wrote:
> "Bob O`Bob" wrote in message
> @TK2MSFTNGP03.phx.gbl...
>>> 2) What is the difference in scope of:
>>> Private Sub S()
>>> Sub S()
>>>
>>> In (2): Nothing, I believe. The absence of a scope qualifier on a
>>> Sub or Function implies Private, right?
>>
>> I think that's the case, but it's not something I'm likely to ever
>> bother to test.
>> VB has, over the years, given me a serious distaste for defaults.
>
> I agree. As a newbie to VB I wanted to explicitly write "Private" if
> that was the default.
Wise instinct. Because Public is actually the default.
Very easy to test. In a FRM file:
Private Sub Command1_Click()
test
End Sub
In a BAS file:
Sub test()
Debug.Print "test"
End Sub
Run, press Command1.
--
Working without a .NET?
http://classicvb.org/
|
|
| Back to top |
|
 |
|
|
| Related Topics: | OCX issues (My apologies if this message has already been posted. I tried earlier and it didn't seem to make it to the NG) I have an OCX that is used to interface to a camera. It was written in-house. IN VB6, under components, I now have three of the same entry. I w
VB / DLL Issues I'm running a device driver DLL from my VB program. Each time I want to access the device, I run an Initialize command to the driver then go about my business reading data and when I am finished with the device I issue a Kill command to the driver. The
VB .NET Upgrade Issues Hi, all I tried to open a VB6 project in VB .NET, which invokes the upgrade engine, and after upgrading, it dumped me with this list of upgrade errors. The project that I tried to open is an example taken from MS directX8.1 SDK vbexamples folder (called P
Version Issues I downloaded what I thought was the latest version of the DirectX 9.0 SDK - The Summer Update 2003 with 9.0b, but when I try to run anything it complains that the pre- release version od DirectX is expired. How can I tell which is the proper SDK download
PCAnywhere OLE issues... I'm trying to use PCAnywhere in my application to transfer files and execute remote commands. I originally tried using the command queue but this won't really work for me because the paths I'm sending and receiving from are not going to remain the same. |
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|