Jump to content
dummzeuch

is it possible to undeclare an identifier?

Recommended Posts

The unit Windows declares quite a few rather generic identifiers, e.g.

const ERROR = 0

which is annoying when you want to use that identifier for a local variable (e.g. a string):

var
  Value: Double;
begin
// function TryGetSomeValue(const _Name: string; out _Value: Double; out _Error: string): boolean;
  if not TryGetSomeValue('WrongName', Value, Error) then begin
    LogError(Error);
    Value := -1;
  end;

This will not compile because Error is a constant and not a string variable. So I want to declare it as a local string variable using Shift+Ctrl+V but that fails (does nothing *1) because there is already such an identifier.

 

This is no problem when it occurs once, but it becomes a bloody nuisance when you have to write several similar function calls.

I would really like to use "Error" as a variable name here, rather than "Err" or "lError" or "ErrorStr".

 

Is there any way to undeclare an identifier, so  Shift+Ctrl+V (and probably others) work again?

I doubt it but I have been wrong before.


 

(*1: Yes, one could regard this as a bug in the refactoring functionality. I haven't checked whether there already is a bug report.)

Share this post


Link to post

not talk about that "Error" is procedure in System.pas, line 5759.

  • RAD Studio 10.3.3 Arch!

 

if using a .INC for easy hide "original type"?

 

A FranksteinBuilders presents:  😂

 

INCLUDE FILE

{$DEFINE ERRORasSTRING}
//
{$IFDEF ERRORasSTRING}

var
  Error: string;
{$ELSE}
// Else, stay the original "Const"
{$ENDIF}

 

in USE:

unit Unit1;

interface

// uses
// ...;

{$I incRedeclareErrorForMyProject.inc } // var Error:string;

// type
// ...
// var
// Error: string;

procedure prcTestUnit2;

implementation

{ ..$I incRedeclareErrorForMyProject.inc }
// var
// Error: string;

procedure prcTestUnit2;
var
  lText: string;
begin
  lText := Error;
  //
  // System.Error( errorCode:TRuntimeError );
end;

end.

of course, if "ERRORasSTRING" not defined, then, the compile help you with many "Error"s :classic_cheerleader:

hug

 

Edited by emailx45

Share this post


Link to post

I know its modern nowadays to use all kind of bare names for anything.

But those names are very likely to conflict with identifiers, keywords, etc., with a whole bunch of possible hard.-to-find sideeffects.

 

Thats why I still like consequently use the decoration of names:
L - for local instance: LError

F - for field instance:  FError

G - for a global instance: GError (nevertheless: global variables are not recommended)

 

This little extra char gives me a lot of extra-info

- its not a function or procedure, if you use it with discipline

- its not a global, intrinsic function, if you use it with discipline

- its a local or field instance, or even global

 

All this just because of the tiny character, I think its still very much worth it.

 

Edited by Rollo62

Share this post


Link to post

I like: "lMyXXXXXX"  l=L  because ALL is "local" on the end!

 

Global = local for my app (or project)

Local = local for my unit/proc/func

 

 When "XXXX" have a reference with type of var, ex.:  lMyFDClient = FDQuery/Table client

 

then, "l" on the start it's good for me!

Edited by emailx45

Share this post


Link to post

another way, as was said in another post, is possible use "&" char before names on vars, like:

 

var
  &Error: string;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Caption := &Error;
end;

NOTE: verify since when it's possible in Delphi editions!

Share this post


Link to post
On 12/5/2020 at 4:58 PM, emailx45 said:

another way, as was said in another post, is possible use "&" char before names on vars, like:

 


var
  &Error: string;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Caption := &Error;
end;

 

 

Yes, thats possible too, and I always wonder why on earth I should ever do it like that ?

Oh yeah: Its so much advanced over FError, to have a fancy pig-tail in front :classic_smile:

Edited by Rollo62

Share this post


Link to post

"F" is for private "Field" on Class/Record/Interfaces etc... definitions, not for common "var" on unit-body by default.

each other use a "fancy pig-tail" that like it

Edited by emailx45

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×