Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix procedure circular reference?

I'm new in the Delphi programming scene and i have trouble calling a procedure in a procedure in my console application.

My simple application is for a item inventory running through a telnet server on windows. I'm using a old thinkpad as my thinclient running linux and a telnet client.

Using Delphi XE I've ran into a chicken or the egg situation.

I get addscreen undeclared indentifier... it is declared but under mainscreen !!! If i put addscreen procedure over the mainscreen one, any call to mainscreen in the addscreen procedure make me an error undeclared indentifier mainscreen!

In simple terms, how to make the procedure to be call everywhere in the program?

I've tried interface and implementation but its not valid in a console application program!

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils, windows, messages, Console in 'Console.pas';


procedure mainscreen;
var
  choice: string;
begin
  clrscr;
  writeln(' ------------------------------------------------------------------------------------------------------------------------------');
  writeln(' |                                     Inventory Management 0.1 ALPHA                                                         |');
  writeln(' ------------------------------------------------------------------------------------------------------------------------------');
  writeln('');
  writeln('');
  writeln('');
  writeln('');
  writeln('');
  writeln(' Make a choice: Add(a), Remove(r), Edit(e), Backup Database(bd), Mass Add(ma), Mass Remove(mr)');
  writeln('?:');
  readln(choice);

  if choice = 'a' then 
    addscreen
  else 
    mainscreen;
end;

procedure addscreen;
var
  choice: string;
begin
  clrscr;
  writeln(' ------------------------------------------------------------------------------------------------------------------------------');
  writeln(' |                                                     Add an Item                                                            |');
  writeln(' ------------------------------------------------------------------------------------------------------------------------------');
  writeln('');
  writeln('');
  writeln('');
  writeln(' Not yet ready!');
  writeln(' Press any key to return to the main menu...');
  readln(choice);

  mainscreen;
end;

begin
  mainscreen;
  textbackground(black);
  textcolor(lightgray);
  ExitProcess(0);
end.

Thank you very much!

like image 239
ELCouz Avatar asked Oct 16 '11 19:10

ELCouz


1 Answers

The compiler passes through the file from top to bottom. At the point where it needs to deal with the call to addscreen, you have not yet declared addscreen.

Since you have a circular reference (addscreen calls mainscreen and vice versa) you need to break that with what is known as a forward reference. Add this line before mainscreen

procedure addscreen; forward;

So the organisation of the code should look like this:

procedure addscreen; forward;

procedure mainscreen;
begin
  ...
end;

procedure addscreen;
begin
  ...
end;

If you did not have a circular reference then you could simply reorder the procedures so that the addscreen was declared before mainscreen.

like image 169
David Heffernan Avatar answered Sep 19 '22 21:09

David Heffernan