1
votes

There are a lot of questions already with this title; however, none address my issue. I am actually trying to pass by reference, yet am receiving the E2033: Types of actual and formal var parameters must be indentical error when trying to compile my code. I am trying to pass three (3) variables, each is an Integer, two by reference (Var) and the other not.

I do not understand the issue with my code, below. I have included the declaration, the definition, and the call.

Declaration of Routine:

private
    updateDeviceStatus(Var aReturnCount, aNotFoundCount: Integer; aNumOfDevices: Integer);

I have tried to not condense the declaration of the arguments and declared Var for the first two, explicitly' however, this did not work.

Question 1: is the error because I am mixing by reference and by value (if I remember, correctly, some languages do not permit this)?

Definition of Routine:

procedure TfrmReturnMeterToMfg.UpdateDeviceStatus(Var aReturnCount, aNotFoundCount: Integer; aNumOfDevices : Integer);
begin
    // DO SOMETHING
end;

Really, the code in the body of the routine is trivial with regard to the problem and does not affect the problem (at least it shouldn't be the cause in this case).

The Call to Routine:

The following is contained within another routine's body:

// local variables:
var ReturnCount, NotFoundCount, NumOfDevices: Integer;
begin

// SOMETHING HAPPENS TO EACH OF THESE VALUES (THEY ARE INCREMENTED)

UpdateDeviceStatus([ReturnCount], [NotFoundCount], NumOfDevices);
end;

Then I receive the error.

Question 2: is this a result of my syntax when calling the routine (attempting to pass the arguments)?

EDIT

So, you may be wondering (you being a more experienced Delphi programmer), "Where did this lark pick up the [ and ] bit? Here's the resource I was consulting (and see why I looked at the wrong thing in the comments, below): consulted resource.

1
Why are you putting [ brackets ] around the arguments in the call? Do you want to pass them as arrays? Why? And why are you calling UodateDeviceStatus when the function name is UpdateDeviceStatus with a p?JensG
@JensG So, I am inferring from your comment that the brackets are not needed? Second, that was a typo on my part when typing in the code to SO. Thanks.Thomas
Yes. I just wanted to confirm which errors are simply typos and which are not. ;-)JensG
Which book are you using that told you to put parameters in brackets?Rob Kennedy
I see. Thank you. Those examples are using ShowMessageFmt. Do your functions look like the declaration of ShowMessageFmt? Pay attention to the "Passing data by reference" section and see the place where the parameter (A) is actually passed by reference. It's not in brackets.Rob Kennedy

1 Answers

4
votes

I guess the brackets are the problem. This code works for me:

program Project12;

{$APPTYPE CONSOLE}

type
 TfrmReturnMeterToMfg = class
 private
   procedure UpdateDeviceStatus(Var aReturnCount, aNotFoundCount: Integer; aNumOfDevices : Integer);
 end;


procedure TfrmReturnMeterToMfg.UpdateDeviceStatus(Var aReturnCount, aNotFoundCount: Integer; aNumOfDevices : Integer);
begin
    // DO SOMETHING
end;

var thing : TfrmReturnMeterToMfg;
    ReturnCount, NotFoundCount, NumOfDevices: Integer;
begin
  ReturnCount := 4;
  NotFoundCount := 2;
  NumOfDevices := 42;

  thing := TfrmReturnMeterToMfg.Create;
  thing.UpdateDeviceStatus( ReturnCount, NotFoundCount, NumOfDevices);
  thing.Free;
end.

Adding brackets around the argument changes the meaning of the code completely, they mean something. In this particular case, by adding brackets you instruct the compiler to pass

UpdateDeviceStatus( 
  [ ReturnCount ],      // array or set of integer (with one element)
  [ NotFoundCount ],    // same again here
  NumOfDevices          // integer
);

This is something completely different.