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.

A structure field or an array element is checked against the type its declaration records, as a variable is. Assigning a STRING to an INT field (t.f := s) or to an element of an ARRAY OF INT (a[1] := s) is this error, as assigning it to an INT variable is.

An assignment through a dereference (p^ := value) stores into the variable the reference refers to, so it is checked against that variable’s type. Assigning a DINT through a REF_TO SINT is this error, as assigning it to the SINT itself is.

An output assignment of a function block call (fb(Q => target)) stores the output in its target after the call, so it is checked as target := fb.Q is. Storing an LINT output in an INT variable is this error. Storing a STRING in a WSTRING, or a WSTRING in a STRING, is P4034 instead, however the value is named.

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.