Solved

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

Posted on 1997-11-10
6
176 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
What is Backup? Backup software creates one or more copies of the data on your digital devices in case your original data is lost or damaged. Different backup solutions protect different kinds of data and different combinations of devices. For e…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now