Recent

Author Topic: Local variable that does not lose its value [SOLVED].  (Read 448 times)

Nera

  • Full Member
  • ***
  • Posts: 103
Local variable that does not lose its value [SOLVED].
« on: July 05, 2020, 11:07:05 pm »
Hi Guys
There is a variable modifier in Pascal that prevents a local variable from losing its value after exiting the method. It would be something like a global variable that does not lose its value, but this variable is local. In C the modifier is "static".

thanks
« Last Edit: July 06, 2020, 01:16:34 pm by Nera »

jamie

  • Hero Member
  • *****
  • Posts: 3540
Re: Local variable that does not lose its value.
« Reply #1 on: July 05, 2020, 11:10:23 pm »
its called a writeable constant..

Const AVariable :Boolean = false;

Begin
….
..
The only true wisdom is knowing you know nothing

Nera

  • Full Member
  • ***
  • Posts: 103
Re: Local variable that does not lose its value.
« Reply #2 on: July 05, 2020, 11:20:08 pm »
its called a writeable constant..

Const AVariable :Boolean = false;

Begin
….
..

I want an integer that changes the value but keeps it after exiting the method. Then, if I enter the method again, the value will be preserved.

jamie

  • Hero Member
  • *****
  • Posts: 3540
Re: Local variable that does not lose its value.
« Reply #3 on: July 05, 2020, 11:23:48 pm »
Ok, but it looks like you are using an old hack that I use

Const Busy:Boolean;

Begin

 if Busy then Exit else Busy := true;

 Your code;;;

Busy := False;

End;

As for the other types you can do that with what ever..

Const  MyInteger :Integer = SomeInitalValue;

The only true wisdom is knowing you know nothing

TRon

  • Hero Member
  • *****
  • Posts: 536
Re: Local variable that does not lose its value.
« Reply #4 on: July 05, 2020, 11:50:34 pm »
I want an integer that changes the value but keeps it after exiting the method. Then, if I enter the method again, the value will be preserved.
Unless you really meant to say something else that is exactly what jamie was suggesting.

But, perhaps you don't believe in miracles ?  :)

Code: Pascal  [Select][+][-]
  1. program preserved_local_var;
  2.  
  3. {$MODE OBJFPC}{$H+}
  4.  
  5. procedure method;
  6. const
  7.   local_var: integer= 0;
  8. begin
  9.   Inc(local_var);
  10.   WriteLn('local_var = ', local_var);
  11. end;
  12.  
  13. var
  14.   n : integer;
  15. begin
  16.   for n := 1 to 10 do method;
  17. end.
  18.  

outputs:
Code: [Select]
local_var = 1
local_var = 2
local_var = 3
local_var = 4
local_var = 5
local_var = 6
local_var = 7
local_var = 8
local_var = 9
local_var = 10

From that you are able to conclude that the local declared variable (writeable constant) is preserving it's value after exiting the method. Exactly as user jamie told it would.

Nera

  • Full Member
  • ***
  • Posts: 103
Re: Local variable that does not lose its value.
« Reply #5 on: July 06, 2020, 01:16:03 pm »
Thank you very much. That was exactly what I was needing.

 

TinyPortal © 2005-2018