[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 253
  • Last Modified:

Where to declare variables when using Procedures held in used units?

I've got a program made up of 20+ units that I am trying to clean up.

Several of them use the same procedure, so it makes sense to put the common routines into a separate unit and call the routines as required. That way the code only exists once.

But these routines have a lot of variables which are declared in the parent unit.

When I compile the shared routine I get lots of "Undeclared Identifier" errors (what a surprise!)

PARENT
Declares the variables
Has a USE to the shared routine
Call the shared routine

ROUTINE
has the common procedure


Where should I declare the variables? Or do I need to make them all global and be done with it?

Ed

procedure LoadPerCentData;
0
edhasted
Asked:
edhasted
1 Solution
 
2266180Commented:
there are several way to overcome this:
- make the variables global in one unit
- move the variables to the untis in whcih tehy are being used

first case:
unit unti1;
interface
uses ...
variables
implementation
uses unti2;
end.

unit unti2;
interface
uses ... unit1;
functions
implementation
implement functions
end.

second case it's pretty straitforward
0
 
atul_parmarCommented:
You can also pass variables as parameter to your routine. This will increase the work and effort but will make your routine generic.

0
 
2266180Commented:
or, if there are many such variables, you can put all of them in a record, declare it global and pass it around the routins (it's a used method in delphi. see for example TFormatSetting structure ;) ). this also allows a better concurrent access control and easier copying the variables between threads/applications. but now I'm getting into too many details/complications
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
BlackTigerXCommented:
I would go with ciuly suggestion, it is very extensible, you just have to pass one variable around (of the type of the record) and if you need a new variable, you just add it to the record and voila, is available
0
 
kfoster11CEOCommented:
Programming 101.  No Globals...  
0
 
edhastedAuthor Commented:
Will work on this over the weekend - many thanks - Ed
0
 
David_WardCommented:
@kfoster11

Globals have their place, and, as has been pointed out, even Borland use them on occasion.
0
 
edhastedAuthor Commented:
Hope to get some time to work on this today - very many thanks for everyone's input - Ed
0
 
edhastedAuthor Commented:
Many thanks to you all - I appreciate the issues with Globals but for this little piece of jobbing code it hit the mark.

With best wishes,

Ed
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now