Solved

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

Posted on 1997-11-10
6
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Resolving an irritating Remote Desktop connection that stops your saved credentials from being used.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

696 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