Skip to main content

Command: Auto Declare

Shortcut: Shift + F2

Function: The command opens the Auto Declare dialog which helps to declare a variable.

Call: Edit menu

Requirement: An object or a device of the project is open in the editor.

With the auto-declaration function, the Auto Declare dialog is also displayed when the cursor is positioned in the implementation part of a POU in a line which contains the name of an undeclared variable. The requirement is that you first have to select the Declare unknown variables automatically (AutoDeclare) option in the Tools → Options menu, in the SmartCoding category.

With the smart tag function, the Auto Declare command is also displayed when you place the cursor over an undeclared variable in the implementation part of the ST editor and then click _cds_icon_light_bulb.png.

Dialog: Auto Declare

Scope

Scope of the variable which is not declared yet

Example: VAR (default setting for a local variable)

Name

Variable name which is not declared yet

Example: bIsValid

Type

Example: BOOL

  • _cds_icon_arrow_down.png: Lists the standard data types

  • _icon_arrow_button.png

    • Input Assistant: Opens the Input Assistant dialog

    • Array Wizard: Opens the Array dialog

Object

Object where the new variable is declared

By default, the object which you are editing right now.

Example: fbA

_cds_icon_arrow_down.png: Lists those objects where the variable can be declared

If no objects are available for the selected Scope, then the <create object> entry is displayed. When you select the <create object> entry, the Add Object dialog opens to generate a suitable object.

Initialization

Example: FALSE

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

_cds_icon_input_assistent.png: Opens the Initialization value dialog

This method is helpful for the initialization of structured variables.

Address

Memory address of the application for the variable which is not declared yet

Example: %IX1.0

Note:

. Possible only for the following scopes:
  • Local variable (VAR)

  • Global variable (VAR_GLOBAL)

  • Persistent variable (PERSISTENT)

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

Example: New input In1

In the tabular declaration editor, the specified comment is displayed in the Comment column, while in the textual declaration editor it is displayed above the variable declaration.

Apply changes using refactoring

_cds_icon_checked.png: When you exit the dialog, the variable is not declared yet, and therefore the Refactoring dialog opens. There you can continue editing your changes.

. The option appears for the following scopes:
  • Input variable (VAR_INPUT)

  • Output variable (VAR_OUTPUT)

  • VAR_IN_OUT variables (input variable and output variable)

OK

The variable is declared and is displayed in the declaration.

Example:

VAR RETAIN
 // New input In1
 xIn1 AT %IX1.0: BOOL := FALSE;
END_VAR

Dialog: Array

Ranks and Base Type Specification

Definition of the field sizes (Dimension) by specifying the upper and lower limits and the Base Type of the array

You can specify the base type directly, or click the _icon_arrow_button.png button for help from the Input Assistant dialog or Array dialog.

Result

Display of the defined array

Important

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

Dialog: Initialization value

List of the variables by name (Expression), Initialization Value and Data Type

Changed initialization values are displayed in bold fonts.

Input field below the list

Input of an initialization value for the selected variable(s)

Apply Value to Selected Lines

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

Reset Selected Lines to Default Values

Resets to the default initialization values

OK

CODESYS applies the initialization values in the Auto Declare dialog.

In the case that the variable to be initialized by means of this dialog is a function block instance with an extended FB_Init method, an additional table is displayed above the Initialization Value table. The additional FB_Init parameters are listed in this table. The meaning and handling essentially correspond to the lower table with the following differences:

  • All variables have to be assigned with initialization values. Otherwise OK remains disabled.

  • For complex data types (structures, arrays), no components contained within are displayed (type cannot be expanded). In this case, the complex type has to be initialized with a corresponding variable.

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

For more information, see: Method: FB_Init, FB_Reinit, FB_Exit and AT Declaration