P4031

Code

P4031

Message

Constant referenced in type parameter is not an integer type

This error occurs when a constant name is used in a type parameter (such as a STRING length or array bound) but the referenced constant is not an integer type.

Permitted constant types in this position include SINT, INT, DINT, LINT, and the unsigned USINT, UINT, UDINT, and ULINT.

Example

The following code will generate error P4031:

VAR_GLOBAL CONSTANT
    MY_CONST : REAL := 3.14;
END_VAR

FUNCTION_BLOCK MyFB
VAR_INPUT
    name : STRING[MY_CONST];
END_VAR
END_FUNCTION_BLOCK

The constant MY_CONST is a REAL type, which cannot be used as a string length. To fix this error, use an integer type:

VAR_GLOBAL CONSTANT
    MY_CONST : INT := 100;
END_VAR

The same restriction applies to array bounds. The following declaration also raises P4031 because the bound is taken from a REAL constant:

VAR_GLOBAL CONSTANT
    BUFFER_SIZE : REAL := 16.0;  (* Wrong type for an array bound *)
END_VAR

FUNCTION_BLOCK Buffer
VAR
    data : ARRAY[1..BUFFER_SIZE] OF INT;  (* Error: bound is REAL, not integer *)
END_VAR
END_FUNCTION_BLOCK

Change BUFFER_SIZE to an integer type such as INT := 16 to resolve the error.

See Also

  • Data Types — elementary integer types accepted here

  • STRINGSTRING length parameter

  • Array Types — array bound expressions

  • P4030 — constant referenced in a type parameter is not defined

  • P4032 — constant referenced in a type parameter has no initializer

References

Think IronPLC is wrong about this?

If you believe this diagnostic is incorrect, open an issue on GitHub with a small sample that demonstrates the problem.