Declare Variable and Declare Field Overview (Delphi)

From RAD Studio
Jump to: navigation, search

Go Up to Refactoring Applications Index

You can use the Refactoring feature to create variables and fields. This feature allows you to create and declare variables and fields while coding without planning ahead. This topic includes information about:


Declare Variable - Shift+Ctrl+V

You can create a variable when you have an undeclared identifier that exists within a procedure block scope. This feature gives you the capability to select an undeclared identifier and create a new variable declaration with a simple menu selection or keyboard shortcut. When you invoke the Declare Variable dialog, the dialog contains a suggested name for the variable, based on the selection itself. If you choose to name the variable something else, the operation succeeds in creating the variable, however, the undeclared identifier symbol (Error Insight underlining) remains.

Variable names must conform to the language rules for an identifier. In Delphi, the variable name:

  • Cannot be a keyword.
  • Cannot contain a space.
  • Cannot be the same as a reserved word, such as if or begin.
  • Must begin with a Unicode alphabetic character or an underscore, but can contain Unicode alphanumeric characters or underscores in the body of the variable name.
  • In the Delphi language, the type name can also be the keyword string.


Note: On the dialog that appears when you choose to declare a variable, you can set or decline to set an initial value for the variable.

Initial Type Suggestion

The refactoring engine attempts to suggest a type for the variable that it is to create. The engine evaluates binary operations of the selected statement and uses the type of the sum of the child operands as the type for the new variable. For example, consider the following statement:

myVar := x + 1;

The refactoring engine automatically assumes the new variable myVar should be set to type Integer, provided x is an Integer.

Often, the refactoring engine can infer the type by evaluating a statement. For instance, the statement If foo Then... implies that foo is a Boolean. In the example If (foo = 5) Then... the expression result is a Boolean. Nonetheless, the expression is a comparison of an ordinal (5) and an unknown type (foo). The binary operation indicates that foo must be an ordinal.

Declare Field - Shift+Ctrl+D

You can declare a field when you have an undeclared identifier that exists within a class scope. Like the Declare Variable feature, you can refactor a field you create in code and the refactoring engine will create the field declaration for you in the correct location. To perform this operation successfully, the field must exist within the scope of its parent class. This can be accomplished either by coding the field within the class itself, or by prefixing the field name with the object name, which provides the context for the field.

The rules for declaring a field are the same as those for declaring a variable:

  • Cannot be a keyword.
  • Cannot contain a space.
  • Cannot be the same as a reserved word, such as if or begin.
  • Must begin with a Unicode alphabetic character or an underscore, but can contain Unicode alphanumeric characters or underscores in the body of the field name.
  • In the Delphi language, the type name can also be the keyword string.

Note: Leading underscores on identifiers are reserved in .NET for system use.

You can select a visibility for the field. When you select a visibility that is not private or strict private, the refactoring engine performs the following operations:

  • Searches to find all child classes.
  • Searches each child class to find the field name.
  • Displays a red error item if the field name conflicts with a field in a descendent class.
  • You cannot apply the refactoring if it conflicts with an existing item name.

Sample Refactorings

The following examples show what will happen when declaring variables and fields using the refactoring feature.

Consider the following code:

TFoo = class
private
  procedure Foo1;
end;
...

implementation

procedure TFoo.Foo1;
begin
  FTestString := 'test';    // refactor TestString, assign field
end;

Assume you apply a Declare Field refactoring. This would be the result:

TFoo = class
private
  FTestString: string;
  procedure Foo1;
end;

If you apply a Declare Variable refactoring instead, the result is:

procedure TFoo.Foo1;
var                            // added by refactor
  TestString: string;    // added by refactor
begin
  TestString := 'test';     // added by refactor
  TestString := 'whatever';
end;

See Also