PlanetSquires Forums

Support Forums => Other Software and Code => Topic started by: Greg Marbach on April 07, 2010, 10:16:58 AM

Title: Declare a variable in a module
Post by: Greg Marbach on April 07, 2010, 10:16:58 AM
I have been programming in VB6 for many years but new to Firefly and Powerbasic. I'm wanting variables for in a module and restricted to that module only. I'm not wanting to use Global.
How can I do this?

Title: Re: Declare a variable in a module
Post by: Rolf Brandt on April 07, 2010, 12:31:19 PM
Hello Greg,

that does not work in PB. The scope of a variable is either global (public in VB) or local (Dim in Sub or Function in VB).

From PB Help File:

Variable Scope
The scope of a variable is defined as its visibility and its lifetime. Visibility means what parts of your program can access it. Lifetime defines when it is created and when it is destroyed. In PowerBASIC, there are many choices of scope to afford the maximum flexibility. You may choose any scope which best fits the needs of your program. When any variable is created in PowerBASIC, it is automatically initialized.  variables are initialized to zero (0). Dynamic strings, Field strings, and ASCIIZ strings are initialized to a length of zero (no characters). Fixed-length strings and UDTs are filled with CHR$(0). PowerBASIC automatically destroys every variable when at the appropriate time, so you never need worry about this type of memory leak.

LOCAL
Local variables are only accessible within a single SUB, FUNCTION, METHOD, or PROPERTY. They are automatically created and initialized each time you enter the procedure. They are automatically destroyed when you exit. This is the default variable scope unless you declare otherwise.

STATIC
Static variables are only accessible within a single SUB, FUNCTION, METHOD, or PROPERTY. They are initialized when your program starts, but retain their value regardless of how many times the procedure is entered and exited. They are destroyed only when the program ends.

GLOBAL
Global variables are accessible from anywhere in your program. They are initialized when your program starts and are destroyed when the program ends.

THREADED
Threaded variables are accessible from anywhere in your program, but each thread within your program will have its own unique copy of them. They are created and initialized when a thread is created. They are destroyed when the thread ends. Threaded variables are commonly called Thread Local Storage (TLS).

INSTANCE
Instance variables are accessible from any method or property in a class. Each object will have its own unique copy of them. They are created and initialized when an object is created. They are destroyed when an object is destroyed.

Title: Re: Declare a variable in a module
Post by: Greg Marbach on April 07, 2010, 05:56:46 PM
Thank you Rolf for the information

Greg
Title: Re: Declare a variable in a module
Post by: Brian Chirgwin on April 08, 2010, 12:25:33 AM
Hi Greg,

In addition to Rolf's comments.

VB has the concept of a "module" which is "compiled" and loaded separately and can contain functions/variables with scope to that module. In VB a form is "loaded" when the first sub / function is called in that form. PB/Firefly does not have this concept.

In PB a module is an include file i.e.  #include <filename>. This works by replacing the line #include <filename> with the text of the filename at the location of the #include statement. The entire code is compiled as one module.

In PB scope is global for subs/functions as well as variables. There is no such thing as form.function. (i.e. Form1.MyFunction) A function that is defined in a firefly form is global in scope (i.e. can be called from anywhere). The form isn't "loaded" first if that function is called.

Hope this helps more than it confuses.
Title: Re: Declare a variable in a module
Post by: Wilko Verweij on April 08, 2010, 09:53:46 AM
Hi,
I come from VB too, and I must confess that I still use 'modules' in Firefly with .bas extension. Actually I don't think there's anything against it. Before the actual compilation Firefly creates one .bas file and handles the conversion of the other .bas files into .inc files.
So it is up to you what you prefer.
Wilko
Title: Re: Declare a variable in a module
Post by: Rolf Brandt on April 08, 2010, 10:07:09 AM
It really makes no difference what extension the module files have - bas or inc. Could be also txt or anything else. The main reason to use bas and inc files is to differentiate between the main file and the modules. Jose's SED editor makes strong use of this.

Important is that you always must use unique names for Subs or Functions, even if they are in different modules. In VB you can use duplicate names for private subs or functions.
Title: Re: Declare a variable in a module
Post by: Brian Chirgwin on April 08, 2010, 03:24:47 PM
That is true. In PB the extension doesn't have meaning .txt, .inc., .bas, .foo, .frm or any other extension you want to use. In VB the extension has meaning.

Title: Re: Declare a variable in a module
Post by: Rolf Brandt on April 09, 2010, 02:05:00 AM
That reminds me of the (a few years ago) popular term "Dll Hell".

In the beginning I found it quite odd not to have variables with the per-module-scope, but basically it is no problem. You get a accustomed to it.
Title: Re: Declare a variable in a module
Post by: Cho Sing Kum on April 09, 2010, 06:38:59 AM
I have MDI apps written in VB6. The ChildForms are instances of the same form. There can be many child forms opened at any time.

Form level public variables are needed. As an example with one particular form public variable is the strDataPath where a data file is loaded from. I access the variable using ActiveForm.strDataPath. There are more.

How should this be done in PB?

Edit: To change to form level public which is I am using in VB6.
Title: Re: Declare a variable in a module
Post by: Rolf Brandt on April 10, 2010, 04:28:26 AM
Yes - a very good example for the usefulness of module scope variables.

The database manager of my Taxi program also is a VB6 MDI application, which uses several instances of the same form to display different database tables.

The only way of copying VB6's behaviour I can think of is using strDataPath as an array, where each instance of the form uses the corresponding index of strDataPath().

I usually mimic a module scope sting variable this way:
I use a non visible label whiche holds the value. That works well for something like a data path. Not very elegant, but it works for me.


But module scope variables would be more elegant and easier to handle.