HomeDelphiDelphi Error – W1015 FOR-Loop variable ‘%s’ cannot be passed as var parameter

Delphi Error – W1015 FOR-Loop variable ‘%s’ cannot be passed as var parameter

Delphi Compiler Error

W1015 FOR-Loop variable ‘%s’ cannot be passed as var parameter

Reason for the Error & Solution

An attempt has been made to pass the control variable of a FOR-loop to a procedure or function which takes a var parameter. This is a warning because the procedure which receives the control variable is able to modify it, thereby changing the semantics of the FOR-loop which issued the call.

program Produce;

  procedure p1(var x : Integer);
  begin
  end;

  procedure p0;
    var
      i : Integer;
  begin
    for i := 0 to 1000 do
      p1(i);
  end;

begin
end.

In this example, the loop control variable, i, is passed to a procedure which receives a var parameter. This is the main cause of the warning.

program Solve;
  procedure p1(x : Integer);
  begin
  end;

  procedure p0;
    var
      i : Integer;
  begin
    i := 0;
    while i <= 1000 do
      p1(i);
  end;


begin
end.

The easiest way to approach this problem is to change the parameter into a by-value parameter. However, there may be a good reason that it was a by-reference parameter in the begging, so you must be sure that this change of semantics in your program does not affect other code. Another way to approach this problem is change the for loop into an equivalent while loop, as is done in the above program.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Delphi Compiler Error X2421 Imported identifier ‘%s’ conflicts with ‘%s’ in ‘%s’ Reason for the Error & Solution This occurs...
Delphi Compiler Error X2367 Case of property accessor method %s.%s should be %s.%s Reason for the Error & Solution No...
Delphi Compiler Error X2269 Overriding virtual method ‘%s.%s’ has lower visibility (%s) than base class ‘%s’ (%s) Reason for the...