variable scoping in fb/pb? 
Author Message
 variable scoping in fb/pb?

I think I just figured out why I'm having such frustrating results with my
firstBasic project:  I am re-using a variable name in procedures as well as
in the global scope.

What are the scoping rules in firstbasic?  How does one declare variables
with scope?

Does it leave the mouth mediciny?

Thanks very much.



Fri, 29 Mar 2002 03:00:00 GMT  
 variable scoping in fb/pb?

Quote:
>I think I just figured out why I'm having such frustrating results with my
>firstBasic project:  I am re-using a variable name in procedures as well as
>in the global scope.

>What are the scoping rules in firstbasic?  How does one declare variables
>with scope?

First of all, if you have a variable that is SHARED throughout your program
then you'll want a sufficiently unique name for that variable so it will NEVER
be confused with a LOCAL variable, period!

A trick I've used is to capitalize the first letters of all variables EXCEPT
when one is SHARED whereupon I start the name with a small 's'. This way you
don't have these 'confusions'. I've got an article addressing this on my web
site if you're interested.

 MyLocalVariable%
sMySharedVariable%
fMyFunction%
 MySubRoutine
tMyTypeStructure

Real men use Listerine Original!

   ____    _    ____      ____  _____
  |  _ \  / \  / ___) __ | ___)(_   _) Don Schullian

  |____//_/ \_\(____/\__/|_|     |_|    www.DASoftVSS.com
  ___________________________________   www.basicguru.com
      Vertical Software Solutions



Sat, 30 Mar 2002 03:00:00 GMT  
 variable scoping in fb/pb?
Hi Ed,

You define scope using the LOCAL SHARED and STATIC keywords within
subs/functions, and set the default scope for variables within
subs/functions by using one of these keywords in the SUB/FUNCTION
statement them selves.

For example:

shared a as long

a = 1
print a
call test
print a

sub test
local a as integer
a = 2
print a
end sub

Result:
1
2
1


Quote:
>I think I just figured out why I'm having such frustrating results with my
>firstBasic project:  I am re-using a variable name in procedures as well as
>in the global scope.

>What are the scoping rules in firstbasic?  How does one declare variables
>with scope?

>Does it leave the mouth mediciny?

>Thanks very much.

Lance
powerbasic Support

-------------------------------------------------------------------------
PowerBASIC, Inc.      | 800-780-7707 Sales | "We put the Power in Basic!"
316 Mid Valley Center | 831-659-8000 Voice | http://www.powerbasic.com



Sat, 30 Mar 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. dynamic arrays in FB/PB?

2. Lexical scoping good, dynamic scoping bad

3. Variable scoping affected by inline assignment

4. Scoping of collection variables in for-loop

5. Question about (apparently) variable scoping ...

6. variable scoping across imports?

7. Scoping for Tkinter Variables.

8. problem with variable scoping

9. Help with variable scoping

10. Module.Scoping.Subroutine call not initializing variable

11. variable scoping problem

12. Bug in Iwidgets when scoping variables in a class

 

 
Powered by phpBB® Forum Software