Solved

Pass by Reference ?

Posted on 1998-12-27
5
214 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

728 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