P4035

Code

P4035

Message

Assignment value type does not match assignment target type

This error occurs when the value of an assignment statement has a type that is not compatible with the type of the variable being assigned to. IronPLC allows implicit widening between integer types and lossless integer-to-real conversion (for example, assigning an INT expression to a REAL variable), but rejects other type mismatches such as real-to-integer, boolean-to-numeric, or narrowing conversions.

A whole array, a structure or an enumeration is never assignable to a variable of an elementary type, so assigning one is this error too. An enumeration’s values are not integers.

For the full set of allowed implicit conversions, see Type Conversions.

Example

The following code will generate error P4035:

PROGRAM main
VAR
    flag  : BOOL;
    value : REAL;
END_VAR
    flag := value * 0.5;  (* Error: REAL expression assigned to BOOL variable *)
END_PROGRAM

The right-hand side value * 0.5 is a REAL expression, but flag is BOOL.

To fix this error, assign a value whose type matches the target, or use an explicit type conversion:

PROGRAM main
VAR
    flag  : BOOL;
    value : REAL;
END_VAR
    flag := value > 0.0;  (* comparison yields BOOL *)
END_PROGRAM

Assigning a whole array to a single variable is the same error:

PROGRAM main
VAR
    readings : ARRAY[1..2] OF DINT;
    latest   : DINT;
END_VAR
    latest := readings;  (* Error: ARRAY[1..2] OF DINT assigned to DINT *)
END_PROGRAM

To fix this error, assign one element of the array:

latest := readings[2];

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.