Solved

Pass by Reference ?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

919 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now