Click to See Complete Forum and Search --> : what does "out of scope" mean?!


hitecbill
07-28-2005, 10:27 AM
(in regards to error handling)

"this statement goes out of scope when control exits out of the procedure in which it is written"

i know what's going on, but what does it mean in english?!

http://img99.imageshack.us/img99/5331/outofscope1cl.gif

buntine
07-28-2005, 09:08 PM
It may be referring to the statement trying to access data/variables that it does not have access to, and are therefore "out of scope".

In strict languages, if you define a variable within a function, it can then not be referenced from the calling environment.

Regards.

hitecbill
07-29-2005, 10:55 AM
thx for the reply,

hmm, well "On Error Then GoTo 0" is what it's referring to...

here's the full text:

"The On Error Then GoTo 0 statement is used to disable error handling for the procedure in which it is written. This statement goes out of scope when control exits out of the procedure in which it is written. Therefore errors occurring from this point onwards will not be handled by the error handling routine"

weird english...i don't see that the "scope" is that it is going out of...

Adamal
08-03-2005, 04:56 PM
Those who program using VBScript may not necessarily be aware of what "scope" means. When a script is executing code that is NOT in a Function or Sub-procedure, it is said to be running in "global scope". For example, a variable dimensioned outside of a Function or a Sub is considered "global" because it is globally accessible by all Functions and Subs:

Dim GlobalVar
GlobalVar = "I am global!"

Sub EchoGlobalVar ()
Response.Write GlobalVar & "<br />"
End Sub

Call EchoGlobalVar() ' Emits "I am global!" to the client

Variables dimensioned INSIDE Functions (or Subs) are considered to be "local" to the Function (or Sub), because they are accessible ONLY from code within the Function (or Sub):

' Assume this to be the continuation of code from above
Sub EchoLocalVar ()
Dim LocalVar
LocalVar = "I am local!"
Response.Write LocalVar & "<br />"
End Sub

Call EchoLocalVar() ' Emits "I am local!" to the client

Response.Write LocalVar & "<br />"

The "Response.Write" emits nothing, because the variable "LocalVar" is only accessible "locally" from within the "EchoLocalVar()" Sub-procedure.

Having a "local" and a "global" scope allows you to re-use variable names within your Functions and Sub-procedures without worrying about it affecting variables with the same names that are in "global" scope.

Dim MyVar
MyVar = 1

Sub ModifyMyVar ()
Dim MyVar ' Note: Uses the same name as the "global" variable
MyVar = 2
End Sub

Response.Write "Before: " & MyVar & "<br />" ' Emits "1" to the client

Call ModifyMyVar() ' ONLY modifies "local" MyVar

Response.Write "After: " & MyVar ' STILL emits "1" to the client

The "On Error Resume Next" and "On Error Goto 0" commands also observe Function (and Sub-procedure) scope. If you issue "On Error Resume Next" from within a Function or Sub, when the Function or Sub exits, an "On Error Goto 0" is automatically issued to remove error trapping. (Note: You should ALWAYS use "On Error Goto 0" to make your code more readable.)

Sub TrapErrors ()
On Error Resume Next
' Do something here...
On Error Goto 0 ' (Optional) remove "local" error trapping
End Sub

If you issue "On Error Resume Next" in global scope, Functions and Subs must also observe the error trapping behavior.

On Error Resume Next

' ModifyMyVar() will observe the error trapping issued above
Call ModifyMyVar()

On Error Goto 0


~Adamal

hitecbill
08-04-2005, 07:35 AM
ok, that's makes sense...

thx adamal...and again buntine...

thx for your help :)