Solved

Memory places in a unit aren't the same as whole program

Posted on 1997-11-10
6
183 Views
Last Modified: 2010-04-16
If I make a unit

ex. --------------------
unit myunit;
interface
uses crt;
var a:string;
procedure myprocedure;
implementation
procedure myprocedure;
begin clrscr;
     writeln('What is a? ');
       readln(a);
       end;
end.
---------------------------
And I use this unit in this program
---------------------------
program myprogram;
uses crt,myunit;
var a:string;
begin myprocedure;
        writeln(a);readln;
end.
---------------------------
If I run this program, everything is alright, except from the (a). What does I have to do that the a is the same in the unit as in the real program???
Thanks.

mar.bol@skynet.be

0
Comment
Question by:babbelaar
  • 3
  • 3
6 Comments
 

Author Comment

by:babbelaar
ID: 1216671
My English is not very good as you see, sorry for it.
0
 
LVL 2

Accepted Solution

by:
mitchell042997 earned 100 total points
ID: 1216672
The reason why that does't work is because the variable a in your unit is local.  Even if this wasn't in a unit, and you did this:

procedure my_proc;
var a:string;
begin
  readln(string);
end;

begin
  my_proc;
  writeln(a);
end.

it would crap out.  To fix your problem you can do one of two things.  Moving your variable a above the interface line in your unit (i.e.:

unit my_unit;
uses crt;
var a:string;

interface
... blah ...

is one way of fixing it.  It is the poor way of fixing it, because fit makes the variable global, and that is bad.  Might as well be writing in BASIC and using gotos.  The *better* way to solve the problem is to pass the variable to the procedure:

procedure my_proc(var a:string);
begin
  readln(a);
end;

will do the trick.  The var is important, because it passes the parameter by reference rather than by value.  I would highly recommend reading up on the differences between passing by reference and value.

Hope this solves your particular problem!  :)
0
 

Author Comment

by:babbelaar
ID: 1216673
If I do it I receive error 55: Interface expected.
The program needs on the second line after unit(.....);
interface.

What am I doing wrong???
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Expert Comment

by:mitchell042997
ID: 1216674
OK, I see what is wrong now.  I was incorrect when I said to move it above the interface line.  If it is below the interface but before the implementation, then it is global.  What you need to do is NOT to declare:

   var a:string;

in your main program.  Since a is already declared in your unit, you need not declare it again.  (Hence an argument against global variables).  So, if you modify your main program to look like this:

uses crt,myunit;
begin
  myprocedure;
  writeln(a);
end.

It will work beautifully.  Again, I would recommend solving this problem using other techniques, but this should provide an adequate solution for your current problem...  :)
0
 
LVL 2

Expert Comment

by:mitchell042997
ID: 1216675
OK, I see what is wrong now.  I was incorrect when I said to move it above the interface line.  If it is below the interface but before the implementation, then it is global.  What you need to do is NOT to declare:

   var a:string;

in your main program.  Since a is already declared in your unit, you need not declare it again.  (Hence an argument against global variables).  So, if you modify your main program to look like this:

uses crt,myunit;
begin
  myprocedure;
  writeln(a);
end.

It will work beautifully.  Again, I would recommend solving this problem using other techniques, but this should provide an adequate solution for your current problem...  :)
0
 

Author Comment

by:babbelaar
ID: 1216676
I'll try tomorrow because my mother says that I have to go to my bed.
It 22.10pm and I live in Belgium.16/m
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Neural Networks with Delphi 3 1,604
Utf8 encode 2 589
Object was open using ADO and Delphi 2007 33 230
indy Tidtcpserver best  way to brodcast message to multiable clients 1 162
Giving access to ESXi shell console is always an issue for IT departments to other Teams, or Projects. We need to find a way so that teams can use ESXTOP for their POCs, or tests without giving them the access to ESXi host shell console with a root …
ConnectWise and their customers need to ensure critical alerts automatically reach the right person at the right time. MSP superheros efficiently respond to these alerts key is providing automatic, intelligent alerting that generates a complete audi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question