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:
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:
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.
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:
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;
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!