Qbasicnews.com
November 27, 2021, 05:36:38 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: Turbo Pascal Units  (Read 4033 times)
aardvark
Senior Member
**
Posts: 228



« on: April 16, 2004, 09:50:12 AM »

A (probably simple) question: How do I make constants and variables from the main unit of by Borland TP 100% global, ie., visible from my units as well as all of my main program?

PS: And sorry if I only post Pascal and C questions here, but I read all the QB stuff, honest!
Logged

In a race between a rock and a pig, don't varnish your clams."   -- "Dilbert"
Antoni Gual
Na_th_an
*****
Posts: 1434



WWW
« Reply #1 on: April 22, 2004, 04:52:34 PM »

I have not used TP for years...
Maybe putting them at the interface section?  Better check the manual...
Logged

Antoni
wizardlife
Na_th_an
*****
Posts: 1456


WWW
« Reply #2 on: April 23, 2004, 12:04:59 PM »

Quote from: "aardvark"
A (probably simple) question: How do I make constants and variables from the main unit of by Borland TP


If you put its declaration in the unit's 'interface' header as opposed to 'implementation', it will be available to everything that 'uses' it. For other units to have access to the variable, it will need to be passed in or the unit included.

The only stuff that really needs to be global is your type definitions, tho. All values really should be passed.
Logged

aardvark
Senior Member
**
Posts: 228



« Reply #3 on: April 24, 2004, 02:39:09 AM »

Thanks.

But will that work for the main unit, which doesn't have 'interface' and 'implementation'?  Or should I put those in the main unit?
Logged

In a race between a rock and a pig, don't varnish your clams."   -- "Dilbert"
wizardlife
Na_th_an
*****
Posts: 1456


WWW
« Reply #4 on: April 25, 2004, 05:00:40 PM »

Quote from: "aardvark"
Thanks.

But will that work for the main unit, which doesn't have 'interface' and 'implementation'?  Or should I put those in the main unit?


By 'main unit', are you meaning 'the program'?

Because the basic principle is that the variable is global within the source file it's declared in, and if it's in the interface, it's also available to any source file that uses the unit.

If, for some reason, you need a variable to be global across three or four units, do this:

Code:

unit globalstuff;

interface

var
   global1 :integer;
   global2 :string;
 
end.



unit unit1;

interface

uses globalstuff;

procedure whatever;
procedure otherwhatever;
 
end.



unit unit2;

interface

uses globalstuff;

procedure whatever1;
procedure otherwhatever2;
 
end.



program myprog

uses globalstuff, unit1, unit2

begin
   { mainline }

end.
Logged

Wizard Genius
Member
*
Posts: 57


WWW
« Reply #5 on: April 27, 2004, 11:01:12 PM »

Here's how a unit is:

Code:

          Unit unitname;
          Interface
                        {Global declarations here}
          Implementation
                        {Non-global stuff here}
                        {Procedures and functions here are global if declared in interface, because you can't make a procedure or function in the interface... but you can declare it there, and THAT makes it global.}


The idea is that the unit can have procedures that call procedures that don't (or shouldn't) get accessed or used by the program.  Yet those procedures (which know how to use the other procedures) can call the other procedures properly, and be called by the program at the same time. I love TP! BTW, if you need to know the file format of a TPU (it's a binary file) go to http://www.wotsit.org/ where they have file formats for every file you could possibly need! I love that site too!
Logged

rogramming is like life - you work first, then play. :bounce: :rotfl: :bounce: :rotfl: :bounce: :rotfl: :bounce: :rotfl: :bounce: :rotfl: :bounce: :rotfl: :bounce:
aardvark
Senior Member
**
Posts: 228



« Reply #6 on: April 28, 2004, 06:06:57 AM »

Cool-- it works.  Thanks all.
Logged

In a race between a rock and a pig, don't varnish your clams."   -- "Dilbert"
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!