Solved

Pass by Reference ?

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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