Solved

Pass by Reference ?

Posted on 1998-12-27
5
213 Views
Last Modified: 2010-04-04
every time i use 'var' (pass by Reference) for my procedure or function the following code give me a error message [Error] (45): Types of actual and formal var parameters must be identical, why ?


Caller
-------
var
  Button1 : TButton;
  Panel1  : TPanel;
Begin
  SetParent(Button1,Panel1);
End;


Procedure
----------
procedure SetParent(var AChild , AParent : TWincontrol);
Begin
  AChild.Parent := AParent;
end;
0
Comment
Question by:qwertyuiopasdfghjkl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 4

Accepted Solution

by:
dwwang earned 30 total points
ID: 1353282
When using var parameters, the Types of actual and formal var parameters must be exactlyidentical, so the correct call is:

SetParent(TWinControl(Button1),TWinControl(Panel1));
0
 
LVL 4

Expert Comment

by:dwwang
ID: 1353283
By the way, I think you shold change the name for the procedure, since setparent is a Win32API's name, so sometimes you can get confused.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1353284
Wang's suggestions were exactly right :)
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1353285
Yes dwwang is right but has missed the point I think!

Neither parameter needs to be var. When passing objects as var parameters you are saying that the reference to the object cannot be chaged, however all members of an object passed by value may be changed.

The SetParent function (ignoring the naming issue) would best be written as:

procedure SetParent(const AChild, AParent : TWincontrol);
    Begin
      AChild.Parent := AParent;
    end;

This also avoids the need for the ghastly unsafe typecasts.

Cheers,

Raymond.

0
 
LVL 4

Expert Comment

by:dwwang
ID: 1353286
Yes, agree with Raymond, actually changing properties of object need not pass as reference.

Since objects are already pointers in Delphi, reference calling is only needed in stuations that you need to "change themselves", e.g.:

procedure CreateControl(var ret,aParent:TWincontrol);
begin
     ret:=TEdit.Create(aParent);
end;

Anyway, I was just answering his original question, sorry for not giving further discussion :-)

Regards,
Wang
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to center only a line in richedit? 4 74
Delphi XE10 Round Image 2 218
QRReport  TQrmemo vertical stretching 1 64
firemonkey Android Listview Sort items 7 95
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question