Solved

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

Posted on 1997-11-10
6
180 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

An article on effective troubleshooting
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 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