Solved

Pass by Reference ?

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

776 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