Scope of non declared variable 
Author Message
 Scope of non declared variable

I know that I should know this, but just want some reassurance.  I'm
trying to debug a program someone else wrote.

If a variable is not declared at all and is used in two sepearte
functions then the variable is created as a variant for the function
it is in and then is freed when the function is exited.  Correct?

Thanks,

Julie Watson



Sun, 07 Feb 1999 03:00:00 GMT  
 Scope of non declared variable



Quote:
>I know that I should know this, but just want some reassurance.  I'm
>trying to debug a program someone else wrote.

>If a variable is not declared at all and is used in two sepearte
>functions then the variable is created as a variant for the function
>it is in and then is freed when the function is exited.  Correct?

Yes, as it passes out of scope it is destroyed.  Unless it
is declared using STATIC..

So for your case, you can have a hundred functions / subs that use
intTemp as a variable, but it is destroyed and created with each instance.

Peter Mikalajunas

http://www.xnet.com/~kd9fb



Tue, 09 Feb 1999 03:00:00 GMT  
 Scope of non declared variable

Quote:

> I know that I should know this, but just want some reassurance.  I'm
> trying to debug a program someone else wrote.
> If a variable is not declared at all and is used in two sepearte
> functions then the variable is created as a variant for the function
> it is in and then is freed when the function is exited.  Correct?

That is correct. I just tested this using VB3:

Sub foo ()
    Debug.Print "x = "; x
    x = 1
End Sub

Sub bar ()
    Debug.Print "x = "; x
    x = 2
End Sub

Sub Main ()
    foo
    bar
End Sub

The result was:

x =
x =

I think it's a bad idea to not use Option Explicit,
but maybe that's just because I'm prone to typos and
can use whatever help VB can give me to catch them!

--

WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!



Tue, 09 Feb 1999 03:00:00 GMT  
 Scope of non declared variable


Quote:

>> I know that I should know this, but just want some reassurance.  I'm
>> trying to debug a program someone else wrote.
>> If a variable is not declared at all and is used in two sepearte
>> functions then the variable is created as a variant for the function
>> it is in and then is freed when the function is exited.  Correct?
>That is correct. I just tested this using VB3:
>Sub foo ()
>    Debug.Print "x = "; x
>    x = 1
>End Sub
>Sub bar ()
>    Debug.Print "x = "; x
>    x = 2
>End Sub
>Sub Main ()
>    foo
>    bar
>End Sub
>The result was:
>x =
>x =
>I think it's a bad idea to not use Option Explicit,
>but maybe that's just because I'm prone to typos and
>can use whatever help VB can give me to catch them!

Joe,
        As far as I know, all FORM level variables, whether they are declared
as a specific data type OR a variant do not retain their values
outside of the procedure they are used in.
For example:

Sub Form_Load()
        Dim DB As Database
        Dim X As String 'Declared but not used in this procedure, so it is
wasted memory usage and is able to be used below as an integer.
        Set DB = OpenDatabase(App.Path & "\mydb.mdb")
End Sub

Sub cmdRead_Click()
        Dim RS As Recordset
        X = 50          <<-- Assign value to undeclared variable (variant assumed)
        MsgBox "The variable 'X' contains " & X & "."     <<-- Displays "The
variable 'X' contains 50."
        Set RS = DB.OpenRecordset("MyTable", dbOpenSnapshot)  <<---- Causes
error - "Object variable not set" because variable 'DB' no longer
holds a value.
End Sub

Sub cmdPrint_Click()
        MsgBox "The variable 'X' contains " & X & "."     <<-- Displays "The
variable 'X' contains ."
End Sub

Please correct me if I'm wrong here somewhere, but these are just
examples of what I have run into in the past.

Ed Phillippe

********************************************************
**  Southern Indiana Bass Fishing / Outdoor Software  **
**    HTTP://www.evansville.net/~dlion/mypage.htm    **
********************************************************



Tue, 09 Feb 1999 03:00:00 GMT  
 Scope of non declared variable

[snip]

Quote:
> Joe,
>    As far as I know, all FORM level variables, whether they are declared
> as a specific data type OR a variant do not retain their values
> outside of the procedure they are used in.

Not quite. If they aren't declared or are declared only inside a
form-level function or sub, they are local to that function or
sub and lose their values when that function or sub exits. If, on
the other hand, they are declared in the form's declaration
section, then all functions or subs defined in that form share
the variables, and they retain their values until the form is
unloaded. A variable declared as Static inside a form, on the
third hand, is shared by all instances of the form and will
retain its value until the program exits.

[snip]

--

WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!



Tue, 09 Feb 1999 03:00:00 GMT  
 Scope of non declared variable

Quote:

>If a variable is not declared at all and is used in two sepearte
>functions then the variable is created as a variant for the function
>it is in and then is freed when the function is exited.  Correct?

Yes, that is correct.  If its in a sub/function, the life is the life
of the sub or function it is in.

I understand this is someones elses code, and you may not have the
luxury but....  Put in Option Explicit in the module, then declare all
the variables.  It will take some time, but I bet you catch more than
just then one bug.

<SOAPBOX>
Why aren't people using the Option Explicit?  Lazy or what?  I too
have the very same problem in a project I inherited.  Come on people,
(that don't use Option Explicit) get with it!!!!  This is first month
programming.  No wonder people still laugh at VB programmers.
</SOAPBOX>

Thank you for listening.  :)

Rick Clark



Tue, 09 Feb 1999 03:00:00 GMT  
 Scope of non declared variable

Quote:
>I understand this is someones elses code, and you may not have the
>luxury but....  Put in Option Explicit in the module, then declare all
>the variables.  It will take some time, but I bet you catch more than
>just then one bug.

I think the original VB had the default set with Option Explicit off. The first
thing I do is set "Require declarations" (or whatever it's called) on so that all
new stuff will have it.
Gary


Wed, 10 Feb 1999 03:00:00 GMT  
 Scope of non declared variable

I could get on your soapbox too.  We actually hired an external
company to develop this program because we already had too much to do.
When I started rewriting it (since it doesn't work correctly) it has
no variable declarations, variables that are used, comments like
"Doesn't use this but I put it here anyway", and 26 page long
procedures.  I learned all this in "Intro to Programming I".  

The reason I am asking this question is because I have gone back and
used option explicit and declared variables.   The application is a
barcoding system and I am trying to parse strings returned via radio
frequency.  The first 9 times the string is returned correctly, but
the 10th time it is half full of garbage characters.  All my instincts
tell me this is a program problem and not a hardware problem because
it is too consistant (10 time only).  I though maybe a variable was
getting too full or something like that and wanted to verify I wasn't
skipping something easy.  

Any suggestions where to look will be really appreaciated!!

Julie



Fri, 12 Feb 1999 03:00:00 GMT  
 Scope of non declared variable



Quote:
>I could get on your soapbox too.  We actually hired an external
>company to develop this program because we already had too much to do.
>When I started rewriting it (since it doesn't work correctly) it has
>no variable declarations, variables that are used, comments like
>"Doesn't use this but I put it here anyway", and 26 page long
>procedures.  I learned all this in "Intro to Programming I".  

I can't pass this by...  If you hired a consulting firm, then you ( your
company this is ) should have taken a good look at the work produced
before signing off on it.  Additionally, most quality consulting firms have
a warranty that goes with their products.  

Quote:
>The reason I am asking this question is because I have gone back and
>used option explicit and declared variables.   The application is a
>barcoding system and I am trying to parse strings returned via radio
>frequency.  The first 9 times the string is returned correctly, but
>the 10th time it is half full of garbage characters.  All my instincts
>tell me this is a program problem and not a hardware problem because
>it is too consistant (10 time only).  I though maybe a variable was
>getting too full or something like that and wanted to verify I wasn't

That would produce a memory error and VB would complain loudly
about it.

Quote:
>skipping something easy.  

>Any suggestions where to look will be really appreaciated!!

Try where the string is being returned.  Set a break point just before
it,  Add a watch to the de{*filter*} and step through the code. Watch the
string in the debug window.  The problem will become evident.

Peter Mikalajunas

http://www.*-*-*.com/ ~kd9fb



Sat, 13 Feb 1999 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Cannot assign a variable declared as a string to a variable declared as data type

2. Calls to Procedures Using VB Declare Statement Apparently Has Scope Issues (Reply to This One)

3. Tabledef object variable lost out of scope

4. scope of variables

5. Scope and Life of variables

6. Scope of Variables Question

7. Lifetime and scope of variables

8. Q: Variable scope in ThisOutlookSession in O 2000

9. Question regarding variable scope and accessability

10. Variable Scope

11. Question regarding variable scope and accessability

12. scope of variables in macro

 

 
Powered by phpBB® Forum Software