Command Auto Declare

Function: The command opens the Auto Declare dialog, which supports the declaration of a variable.

Call: Edit menu; Editor window context menu

Requirement: A POU is open in the editor and the cursor is in a program line.

The auto-declaration function opens the Auto Declare dialog even if the cursor is in the implementation part of a POU in a line that contains the name of an undeclared variable. To do this, you must have activated the option Declare unknown variables automatically (AutoDeclare) in the TwinCAT options (Extras > Options > TwinCAT > PLC Programming Environment > Smart Coding).

Due to the smart tag feature, the command Auto Declare also appears if you place the cursor on a variable that has not been declared in the implementation part of the ST editor and then click Command Auto Declare 1: (available from build 4026).

Auto Declare dialog

Command Auto Declare 2:

Scope

Validity range of the variables that have not yet been declared.

Example: VAR (default setting for local variable)

Name

Variable name not yet declared

Example: bVar

Data type

Command Auto Declare 3: : Lists the standard data types.

Command Auto Declare 4: :

  • Input Assistant: Opens the Input Assistant dialog
  • Array assistant: Opens the Array dialog

Example: BOOL

Object

Object in which the new variable is declared. By default, this is the object you are editing.

Command Auto Declare 5: : Lists the objects in which the variable can be declared.

If no objects are available for the selected scope, the entry <Create object> appears. If you select the entry <Create object>, the dialog Add object opens, which can be used to create a suitable object.

Initialization value

If you do not enter an initialization value, the variable is initialized automatically.

Command Auto Declare 6: : Opens the Initialization dialog. This approach is helpful for initializing structured variables.

Example: FALSE

Address

Memory address (see PLC documentation: Addresses)

Example: %IX1.0

Flags

Attribute keywords

  • CONSTANT: Keyword for a constant
  • RETAIN: Keyword for a remanent variable
  • PERSISTENT: Keyword for a persistent variable (stricter than RETAIN)

The selected attribute keyword is added to the variable declaration.

Comment

The tabular declaration editor displays the comment that was entered in the Comment column. In the textual declaration editor, it is displayed above the variable declaration.

Example: New variable

Apply changes using refactoring

This option appears for the following validity ranges:

  • Input variable (VAR_INPUT)
  • Output variable (VAR_OUTPUT)
  • Input and output variable (VAR_IN_OUT)

This option is automatically enabled if the autodeclaration options On renaming variables and On adding or removing variables, or on changing the scope are enabled in the TwinCAT options (Tools > Options > TwinCAT > PLC Environment > Refactoring) (see Dialog Options - Refactoring).

If this option is enabled, the variable is not yet declared when the dialog is closed. Instead, the Refactoring dialog opens, in which you can edit the changes further.

OK

The variable is declared and appears in the declaration.

Example:

VAR
    // New variable
    bVar: BOOL := FALSE;
END_VAR

See also:

Array dialog

Command Auto Declare 7:

Ranks and base type specification

Definition of the field sizes (dimension) by entering the lower and upper bounds and the base type of the array. You can enter the base type directly or by using the Input Assistant or Array dialog, if you click the Command Auto Declare 8: button.

Result

Display of the defined array.

Command Auto Declare 9:

TwinCAT only reinitializes variables if you have changed the initialization values of the variables.

Initialization Value dialog

Command Auto Declare 10:

Listing of the variables with name (expression), initialization value and data type.

Changed initialization values are shown in bold.

Input field below the list

Enter an initialization value for the selected variable(s).

Apply value to selected lines

Change the initialization value of the selected line(s) according to the value of the input field.

Reset selected lines to default values

Setting the default initialization values.

OK

TwinCAT adopts the initialization values in the Auto Declare dialog.

If the variable to be initialized via this dialog is a function block instance with extended FB_Init method, another table is displayed above the Initialization value table (see PLC documentation: Methods FB_init, FB_reinit and FB_exit). This table lists the additional FB_Init parameters. The meaning and operation is basically the same as the table below with the following differences:

For FB_Init parameters configured in this way, a corresponding symbol is displayed behind the initialization value in the Auto Declare dialog.

See also: