Command ‘Declare variable’

Keyboard shortcut: Shift+F2

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

Call: Menu bar: Edit

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

Due to the auto-declaration function, the dialog Declare variable also appears when the cursor is located in the implementation part of a POU in a line containing the name of an undeclared variable. The requirement for this is that you must have selected the command Tools ‣ Options and activated the option Automatically declare unknown variables (AutoDeclare) in the category Smart Coding.

Dialog ‘Declare variable’

Validity range

Validity range of the variable that is not declared yet.

Example: VAR (default setting for a local variable)

Name

Variable name that is not declared yet

Example: bIsValid

Data type

Example: BOOL

  • : Lists the standard data types.
    • Input assistant: Opens the Input assistant dialog
    • Array assistant: Opens the Array dialog
Object

Object where the new variable is declared. By default, the object that you are editing now.

Example: fbA

: Lists that objects where the variable can be declared.

If no objects are available for the selected Validity range, the entry <create object> appears. If you select the entry <create object>, the dialog Add object appears for the generation of a suitable object.

Initialization value

Example: FALSE

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

: Opens the Initialization value dialog. This procedure is helpful for the initialization of structured variables.

Address

Memory address of the application for the variable that is not declared yet.

Example: %IX1.0

Notice:

Possible only for the following validity ranges:

  • Local variable (VAR)
  • Global variable (VAR_GLOBAL)
  • Or for a 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 comment entered is displayed in the column Comment, while in the textual declaration editor it is displayed above the variable declaration.

Apply changes by means of refactoring

: If you exit the dialog, then the variable is not declared yet, but then it opens the Refactoring dialog. You can continue editing your changes here.

The option appears for the following validity ranges:

  • Input variable (VAR_INPUT)
  • Output variable (VAR_OUTPUT)
  • VAR_IN_OUT variables (input variable and output variable)
OK

The variable is declared and appears in the declaration.

Example:

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

END_VAR

Dialog ‘Array’

Specification of dimension and basic type Definition of the field sizes (dimension) by entering the lower and upper limits and the basic type of the array. You can enter the basic type directly or with the help of the dialogs Input assistant or Array if you click the button.
Result Display of the defined array.

Hint

CODESYS only re-initializes variables if you have modified the initialization values of the variables.

Dialog ‘Initialization value’

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

Modified initialization values are displayed in bold lettering.

Input field below the list Input of an initialization value for the selected variable(s).
Apply value to the 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 Establishment of the standard initialization values.
OK CODESYS applies the initialization values in the dialog Declare variable.

See also