GLBasic forum

Main forum => GLBasic - en => Topic started by: François Vanzeveren on 2010-Jul-21

Title: Variable local to a specific source file
Post by: François Vanzeveren on 2010-Jul-21
Hello,

Is it possible to define variables local to a source file, i.e. only accessible by functions defined in the same source file?

Thank you.

François
Title: Re: Variable local to a specific source file
Post by: MrTAToad on 2010-Jul-21
Unfortunately no - a GLOBAL in a source files is accessible everwhere.  LOCAL can only be used in the main file or functions...

One way around it would be to use extended types...
Title: Re: Variable local to a specific source file
Post by: Slydog on 2010-Jul-21
I've looked for this answer before too.
It would be nice to have a 'PRIVATE' variable declaration command or something that defines variables global for that file only.
Ha, or how about a 'SCOPE' / 'ENDSCOPE' keyword pair to wrap around your entire file, or use inside of functions to allow variables to be scoped for sections of the function.

But, I just live with the fact that they are global everywhere, not a big deal really, just keep your variable names clear.
Title: Re: Variable local to a specific source file
Post by: Slydog on 2010-Jul-21
Quote from: MrTAToad on 2010-Jul-21
One way around it would be to use extended types...

I've never used extended types yet as I thought I read they can't access each other between different files.

And if that's not an issue, how would you use them for locally global variables?
Would you treat each file as an entire TYPE, similar to a Class?
Title: Re: Variable local to a specific source file
Post by: MrTAToad on 2010-Jul-21
Yes, treat the type like a class - variables will be local to that.

It makes things a bit neater too :)
Title: Re: Variable local to a specific source file
Post by: Kitty Hello on 2010-Jul-22
just prefix it with the name of your library:
GLOBAL gZip_Whatever%
Title: Re: Variable local to a specific source file
Post by: MrTAToad on 2010-Jul-22
I was incorrect about GLOBALS : Defined in the main file and they are accessible anywhere, but defined a source file and they can only be accessed in that file.
Title: Re: Variable local to a specific source file
Post by: François Vanzeveren on 2010-Jul-22
Quote from: MrTAToad on 2010-Jul-22
I was incorrect about GLOBALS : Defined in the main file and they are accessible anywhere, but defined a source file and they can only be accessed in that file.

I'll try it. That would be great to make code cleaner and easier to read!

Thank you!