Add Local Variable: declare local var in Delphi and C#.

The Add Local Variable refactoring is used to declare a local variable while you're entering code in a method or procedure. The refactoring picks up the word at cursor position and determines the variable's type based on history and context. An Add Local Variable dialog is used to optionally change the suggested type.

An example

Suppose you have a method TSample.DoSomething like this and need a local variable S: string.

procedure TSample.DoSomething;
begin
  // press Ctrl+L
  S :=
end;

Rather than manually adding the local variable, press Ctrl+L (default keyboard shortcut) to invoke the Add Local Variable refactoring:

Add Local Variable Dialog

The Add Local Variable dialog shows the picked up identifier "S" and "string" type (here based on history). Just hitting the enter key is enough to accept the suggestion and actually add the variable declaration without scrolling.

Refactor

procedure TSample.DoSomething;
var
  // inserted variable declaration
  S: string;
Refactor Result
begin
  S :=
end;

In effect you need just two keystrokes to add a local variable: Ctrl+L to invoke the dialog and Enter to accept the suggested type.

If you need to change the suggested type, the type selector has incremental search and just pressing the "i" key changes the type to "Integer", pressing the "b" key changes the type to "Boolean". The presets in the type selector are customizable.

Advanced Variable Detection

Rather than just looking for the word at cursor position, Add Local Var looks for assignment statements Identifier := Initializer on a cursor line. It then picks up the variable name as the identifier that is being assigned to. For example, if you type any of the following:

List
List :=
List := TList
List := TList.Create;

and invoke Add Local Variable, the identifier "List" is suggested as variable, rather than the word at the cursor. This saves moving the cursor back to the variable before invoking Add Local Variable. Only if there's a single identifier before the := (assignment) this is assumed the variable. In all other cases the word at cursor position is used as variable name.

In addition, the for loop is also recognized:

for I := 0 to Items.Count - 1 do

Pressing Ctrl+L anywhere in this line suggests to add the loop control variable "I".

In situations like this

IsFound := FindItem(Key, Index);

"IsFound" will be suggested as local variable, regardless of the cursor position because it's left of the assignment := . This makes it impossible to add a local "Index" To avoid this, there's an escape: if the selection is non-empty, the selection is used as variable name rather than the identifier before the assignment.

How the suggested type is determined

Context

The current line is scanned for code like "typename.Create" and "as typename" to suggest a type. For example, invoked on "Stream" in the example below...

procedure TSample.DoSomething;
begin
  // position cursor at Stream and press Ctrl+L
  Stream := TMemoryStream.Create;
end;

Add Local Variable auto suggests "TMemoryStream" which results in:

procedure TSample.DoSomething;
var
  // inserted variable declaration
  Stream: TMemoryStream;
Refactor Result
begin
  Stream := TMemoryStream.Create;
end;

String literals and True|False are also recognized. For example:

"IsValid := False;" // suggests "IsValid: Boolean".
"AName := 'some text';" // suggests "AName: string".

Hungarian Type Prefix

If enabled, a user definable Hungarian type prefix lookup list is used to suggest a type based on case sensitive prefix. For example, with a prefix lookup defined as:

sz=PChar
s=string
d=Double

... a variable "sCaption" would be suggested as string, szCaption as PChar and dValue as Double etc.

History

If you - like many others - always use the same name for a certain type of variable such as S: string; or Doc: TXMLDocument, that name will be suggested next time you add a local variable.