Read-only variables 
Author Message
 Read-only variables

How can I declare a read-only variable in a unit? Is that possible, or
just another compiler trick by the Borland people, like the optional
parameters in procedure headers?

Oh, you are going to say, a variable should be altered. Yes, of course,
but only by the procedures and functions of the same unit, not from
within the main program or any other unit.



Wed, 18 Jun 1902 08:00:00 GMT  
 Read-only variables
In comp.lang.Pascal.borland, Frederic uttered:
Quote:
>How can I declare a read-only variable in a unit? Is that possible, or
>just another compiler trick by the Borland people, like the optional
>parameters in procedure headers?

>Oh, you are going to say, a variable should be altered. Yes, of course,
>but only by the procedures and functions of the same unit, not from
>within the main program or any other unit.

If you do not want the variable to be visible from outside the unit, the
nput it in the implementation part of the unit rather than the interface
and write a function that will return the value of the variable.

The variable can then be easily changed from within the unit but no-one
from outside can make a change.

--
Pedt Scragg

No-one is completely useless, they can always be a bad example.



Wed, 18 Jun 1902 08:00:00 GMT  
 Read-only variables

Quote:

> How can I declare a read-only variable in a unit? Is that possible, or
> just another compiler trick by the Borland people, like the optional
> parameters in procedure headers?

> Oh, you are going to say, a variable should be altered. Yes, of course,
> but only by the procedures and functions of the same unit, not from
> within the main program or any other unit.

This is a feature of OOP, that can't really be done with regular variables.
The closest you could do is this:

unit SomeUnit;

interface;

function MyVal : integer;
{...}

implementation

var
  HiddenVal : integer;

{...}

function MyVal : integer;

begin
  MyVal := HiddenVal;
end;

{...}

begin
  HiddenVal := 0;
end.

In most cases, you would be able to use "MyVal" like you would any other
variable.  In the case of exceptions, you could assign it to a temporary
variable and use that locally; also, if you actually try to assign a value to
MyVal, it won't compile.

--
Scott Earnest            | SPAM protection in effect. Remove  |





Wed, 18 Jun 1902 08:00:00 GMT  
 Read-only variables

{snip}

Quote:
>If you do not want the variable to be visible from outside the unit, the
>nput it in the implementation part of the unit rather than the interface
>and write a function that will return the value of the variable.

>The variable can then be easily changed from within the unit but no-one
>from outside can make a change.

That's not his point!!! He wants the variable to be VISIBLE but not
CHANGEABLE.  Now, how to do it is another matter....
Quote:

>--
>Pedt Scragg

>No-one is completely useless, they can always be a bad example.



Wed, 18 Jun 1902 08:00:00 GMT  
 Read-only variables

Quote:


> {snip}

> >If you do not want the variable to be visible from outside the unit, the
> >nput it in the implementation part of the unit rather than the interface
> >and write a function that will return the value of the variable.

> >The variable can then be easily changed from within the unit but no-one
> >from outside can make a change.

> That's not his point!!! He wants the variable to be VISIBLE but not
> CHANGEABLE.  Now, how to do it is another matter....

Make it a function.

Wouter Ras.



Wed, 18 Jun 1902 08:00:00 GMT  
 Read-only variables
In comp.lang.pascal.borland, Nik Synytskyy uttered:

Quote:


>{snip}

>>If you do not want the variable to be visible from outside the unit, the
>>nput it in the implementation part of the unit rather than the interface
>>and write a function that will return the value of the variable.

>>The variable can then be easily changed from within the unit but no-one
>>from outside can make a change.

>That's not his point!!! He wants the variable to be VISIBLE but not
>CHANGEABLE.  

Can only be done using a function as I described whether he wants it a
different way or not.

Quote:
>Now, how to do it is another matter....

Function as I said before.

--
Pedt Scragg

No-one is completely useless, they can always be a bad example.



Wed, 18 Jun 1902 08:00:00 GMT  
 Read-only variables

Quote:

>Function as I said before.

>--
>Pedt Scragg

Yep.  That's the way to go.


Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Reading REAL variable

2. How to read MSDOS variables from TP 4.0

3. Was - Reading REAL Variable

4. Reading REAL Variable

5. Byte by byte read into variables...HOW?

6. Byte by byte read into variables...HOW?

7. Reading different size variables from same open file in Turbo Pascal 3.0

8. Pascal refuses to read a string variable...

9. How can I read text files into variables?

10. Read variables from text file?

11. Help variable reads t

12. Help variable reads too far THANX to anyone who tries to help

 

 
Powered by phpBB® Forum Software