Posts: 222
Threads: 23
Joined: May 2003
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"
Posts: 1,407
Threads: 117
Joined: Dec 2002
I have not used TP for years...
Maybe putting them at the interface section? Better check the manual...
Antoni
Posts: 720
Threads: 46
Joined: Oct 2001
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.
Posts: 222
Threads: 23
Joined: May 2003
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"
Posts: 720
Threads: 46
Joined: Oct 2001
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.
Posts: 57
Threads: 6
Joined: Apr 2004
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:
Posts: 222
Threads: 23
Joined: May 2003
Cool-- it works. Thanks all.
In a race between a rock and a pig, don't varnish your clams." -- "Dilbert"