Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Turbo Pascal Units
#1
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!
In a race between a rock and a pig, don't varnish your clams." -- "Dilbert"
Reply
#2
I have not used TP for years...
Maybe putting them at the interface section? Better check the manual...
Antoni
Reply
#3
Quote: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.
Reply
#4
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?
In a race between a rock and a pig, don't varnish your clams." -- "Dilbert"
Reply
#5
Quote: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.
Reply
#6
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!
rogramming is like life - you work first, then play. :bounce: :rotfl: :bounce: :rotfl: :bounce: :rotfl: :bounce: :rotfl: :bounce: :rotfl: :bounce: :rotfl: :bounce:
Reply
#7
Cool-- it works. Thanks all.
In a race between a rock and a pig, don't varnish your clams." -- "Dilbert"
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)