Recent

Author Topic: Gobal and Local variable identifier  (Read 6566 times)

kaedusoft

  • New Member
  • *
  • Posts: 12
Gobal and Local variable identifier
« on: October 24, 2018, 11:52:52 pm »
Hi. I have a very simple question but I have not been able to solve it in Pascal. Obviously I know that when a local variable (within a procedure or function) has the same name (identifier) ​​as a global variable, the local variable takes precedence and overshadows the global variable. My question is: Is there any way to overcome this and see a global variable with the same name as a local variable? In languages ​​like C this is possible, but I can not find how to do it in Pascal.

Thanks you very much for your help and your time.

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: Gobal and Local variable identifier
« Reply #1 on: October 25, 2018, 12:08:48 am »
My question is: Is there any way to overcome this and see a global variable with the same name as a local variable?

Nope, the variable in local scope hides completely the global variable unless it's in another (used) unit and you fully qualify the reference, as in uglobals.MyVar := ...
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12411
  • Debugger - SynEdit - and more
    • wiki
Re: Gobal and Local variable identifier
« Reply #2 on: October 25, 2018, 12:09:33 am »
You can prefix the global var with the unit name of the unit in which it is defined.
Unless you have other identifiers (eg vars) that much the unit name.

Code: Pascal  [Select][+][-]
  1. UnitName.Globalvar

Works within the current unit too.

jamie

  • Hero Member
  • *****
  • Posts: 7768
Re: Gobal and Local variable identifier
« Reply #3 on: October 25, 2018, 12:22:37 am »
I requested a compiler enhancement a while ago to where you could just place a DOT to the left without the unit name and it
would simply step back in scope. I guess that didn't fly.. You can do it in C/C++  :D
The only true wisdom is knowing you know nothing

kaedusoft

  • New Member
  • *
  • Posts: 12
Re: Gobal and Local variable identifier
« Reply #4 on: October 25, 2018, 01:53:32 am »
Thank you. Using unit.variable works well, even in the main program using projectName.variable. The most curious thing is that I knew how to use this for the units, but I never realized to use it in the main program.

Thank you very much for your time.

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4715
  • I like bugs.
Re: Gobal and Local variable identifier
« Reply #5 on: October 25, 2018, 06:40:03 pm »
I requested a compiler enhancement a while ago to where you could just place a DOT to the left without the unit name and it
would simply step back in scope. I guess that didn't fly.. You can do it in C/C++  :D
That sounds counter-intuitive and prone to errors. C++ may not be the best example for clear and readable code. I am happy your request did not fly. :-)
Why such tricks are needed? Why not just give unique names for variables?
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

jamie

  • Hero Member
  • *****
  • Posts: 7768
Re: Gobal and Local variable identifier
« Reply #6 on: October 25, 2018, 10:51:05 pm »
Don't knock it until you try it..

using a single "." to back step into the global spec of the current unit would be the same as simply
specifying the current unit in scope, just a lot shorter..

 But I am glad you are happy now, we wouldn't want any disgruntle coders on site.

 Have a peachy day and may your successes be many!

 Oh, did I forget to mention that I am glad you are happy, it makes me happy, too!
 
 73's
The only true wisdom is knowing you know nothing

creaothceann

  • Sr. Member
  • ****
  • Posts: 375
Re: Gobal and Local variable identifier
« Reply #7 on: October 26, 2018, 12:26:34 am »
This is also possible with absolute:

Code: [Select]
unit u;

var
        b  : byte;
        b_ : byte absolute b;

procedure p(const b : byte);
begin
b_ := b + 1;
end;

begin
p(2);
WriteLn(b);  // 3
end.

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Gobal and Local variable identifier
« Reply #8 on: October 26, 2018, 09:55:57 am »
Hi. I have a very simple question but I have not been able to solve it in Pascal. Obviously I know that when a local variable (within a procedure or function) has the same name (identifier) ​​as a global variable, the local variable takes precedence and overshadows the global variable. My question is: Is there any way to overcome this and see a global variable with the same name as a local variable? In languages ​​like C this is possible, but I can not find how to do it in Pascal.

Thanks you very much for your help and your time.
What's the problem to use different names:
class :  fname
global / public: gname
local : name
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Thaddy

  • Hero Member
  • *****
  • Posts: 19268
  • Glad to be alive.
Re: Gobal and Local variable identifier
« Reply #9 on: October 26, 2018, 10:05:01 am »
In languages ​​like C this is possible, but I can not find how to do it in Pascal.

Thanks you very much for your help and your time.
In C this is also not possible.... Maybe you confuse it with case sensitivity of C. In that case the variables actually do not have the same name, C is case sensitive. So variable Test and TEST are different.
In Pascal you need to have two different names, because Pascal is NOT case sensitive, so variable Test and TEST are the same.

Note that it is possible to have and access with the same name if they are not in the same unit: you can use unit scope, unit1.Test vs Test used in any other unit as a local.
objects are fine constructs. You can even initialize them with constructors.

Zoran

  • Hero Member
  • *****
  • Posts: 1988
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Gobal and Local variable identifier
« Reply #10 on: October 26, 2018, 11:38:07 am »
Note that it is possible to have and access with the same name if they are not in the same unit: you can use unit scope, unit1.Test vs Test used in any other unit as a local.

As Martin said, works with same unit too.

Interestingly, I tested and it works not only in units, but also in main program this way:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. // Tested with these modes, works same:
  4. {$mode fpc}
  5. //{$mode objfpc}
  6. //{$mode delphi}
  7.  
  8. uses
  9.   SysUtils;
  10.  
  11. var
  12.   X: LongInt;
  13.  
  14.   function Test22: AnsiString;
  15.   var
  16.     X: AnsiString;
  17.   begin
  18.     X := 'TestProject'; // local variable
  19.     Project1.X := 22; // global variable
  20.  
  21.     Exit(X + Project1.X.ToString);
  22.   end;
  23.  
  24. begin
  25.   writeln(Test22);
  26.   writeln(X); // assigned in Test22 call.
  27. end.
  28.  

output:
Quote
TestProject22
22
« Last Edit: October 26, 2018, 11:44:53 am by Zoran »
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

 

TinyPortal © 2005-2018