Solved

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

Posted on 1997-11-10
6
184 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: 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: 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

Suggested Solutions

The world seems to conceive of a curious bubble separating IT from “the business.”  More so than just about any other pursuit in the commercial world, people think of IT as some kind of an island.
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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