Delphi and Kylix

Moderators: pritaeas
Number of threads: 7264
Number of posts: 19073

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Params by value / reference Posted by dve83 on 2 Jun 2006 at 12:57 AM
Why does the Editbox not react the same as an Integer value when I send it to the procedure as a parameter by value? WHen sending a parameter by value, its not suppose to change outside the procedure, however when sending a component -> it does? see code. I cant get my head around it. thx so long! note:code was written in notepad, excuse any syntax errors.

unit unitname;
interface
uses xxx,zzz;

type
  TForm1 = Class(TForm)
    Edit1:TEdit
  private
   Value:integer;
   procedure CHangeInt(aValue:integer);
   procedure ChangeIntVAR(var avalue:integer);
   procedure ChangeEdit(aEdit:TEdit);
   procedure CHangeEditVAR(aEdit.TEdit);
  public
  end;

var
  Form1: TForm1;

procedure DoStuff();

implementation

procedure DoStuff();
begin
  Value := 0;
  Edit1.Text := '0';
  
  ChangeInt(Value);ShowMessage(inttostr(value));   //result = 0
  CHangeintVAR(value);ShowMessage(inttostr(value));//result =2
  ChangeEdit(Edit1);ShowMessage(Edit1.Text);       //result='1'; <===WHY?!!?
  ChangeEditVAR(Edit1);ShowMessage(Edit1.Text);    //Result='2';
  
end;

procedure TForm1.CHangeInt(aValue:integer);
begin
  aValue:=1;
end;

procedure TForm1.ChangeIntVAR(var avalue:integer);
begin
  aValue:=2;
end;

procedure TForm1.ChangeEdit(aEdit:TEdit);
begin
  aEdit.Text := '1';
end;

procedure TForm1.CHangeEditVAR(var aEdit.TEdit);
begin
  aEdit.Text := '2';
end;

Report
Re: Params by value / reference Posted by zibadian on 2 Jun 2006 at 1:13 AM
: Why does the Editbox not react the same as an Integer value when I send it to the procedure as a parameter by value? WHen sending a parameter by value, its not suppose to change outside the procedure, however when sending a component -> it does? see code. I cant get my head around it. thx so long! note:code was written in notepad, excuse any syntax errors.
:
:
: unit unitname;
: interface
: uses xxx,zzz;
: 
: type
:   TForm1 = Class(TForm)
:     Edit1:TEdit
:   private
:    Value:integer;
:    procedure CHangeInt(aValue:integer);
:    procedure ChangeIntVAR(var avalue:integer);
:    procedure ChangeEdit(aEdit:TEdit);
:    procedure CHangeEditVAR(aEdit.TEdit);
:   public
:   end;
: 
: var
:   Form1: TForm1;
: 
: procedure DoStuff();
: 
: implementation
: 
: procedure DoStuff();
: begin
:   Value := 0;
:   Edit1.Text := '0';
:   
:   ChangeInt(Value);ShowMessage(inttostr(value));   //result = 0
:   CHangeintVAR(value);ShowMessage(inttostr(value));//result =2
:   ChangeEdit(Edit1);ShowMessage(Edit1.Text);       //result='1'; <===WHY?!!?
:   ChangeEditVAR(Edit1);ShowMessage(Edit1.Text);    //Result='2';
:   
: end;
: 
: procedure TForm1.CHangeInt(aValue:integer);
: begin
:   aValue:=1;
: end;
: 
: procedure TForm1.ChangeIntVAR(var avalue:integer);
: begin
:   aValue:=2;
: end;
: 
: procedure TForm1.ChangeEdit(aEdit:TEdit);
: begin
:   aEdit.Text := '1';
: end;
: 
: procedure TForm1.CHangeEditVAR(var aEdit.TEdit);
: begin
:   aEdit.Text := '2';
: end;
: 

:
Objects are passed as pointers. This means that the aEdit is a pointer, which holds several other values. If you change a property then the object pointer isn't changed, only a part of the memory to which it points. Since that memory part is located in the heap instead of the procedure stack, it will be changed globally.
In case of simple types (integers, floats, strings, etc), the value is copied from the heap into the procedure stack. If that value is changed, the heap copy isn't updated, unless the parameter was variable.
Report
Re: Params by value / reference Posted by dve83 on 2 Jun 2006 at 1:19 AM
: : Why does the Editbox not react the same as an Integer value when I send it to the procedure as a parameter by value? WHen sending a parameter by value, its not suppose to change outside the procedure, however when sending a component -> it does? see code. I cant get my head around it. thx so long! note:code was written in notepad, excuse any syntax errors.
: :
: :
: : unit unitname;
: : interface
: : uses xxx,zzz;
: : 
: : type
: :   TForm1 = Class(TForm)
: :     Edit1:TEdit
: :   private
: :    Value:integer;
: :    procedure CHangeInt(aValue:integer);
: :    procedure ChangeIntVAR(var avalue:integer);
: :    procedure ChangeEdit(aEdit:TEdit);
: :    procedure CHangeEditVAR(aEdit.TEdit);
: :   public
: :   end;
: : 
: : var
: :   Form1: TForm1;
: : 
: : procedure DoStuff();
: : 
: : implementation
: : 
: : procedure DoStuff();
: : begin
: :   Value := 0;
: :   Edit1.Text := '0';
: :   
: :   ChangeInt(Value);ShowMessage(inttostr(value));   //result = 0
: :   CHangeintVAR(value);ShowMessage(inttostr(value));//result =2
: :   ChangeEdit(Edit1);ShowMessage(Edit1.Text);       //result='1'; <===WHY?!!?
: :   ChangeEditVAR(Edit1);ShowMessage(Edit1.Text);    //Result='2';
: :   
: : end;
: : 
: : procedure TForm1.CHangeInt(aValue:integer);
: : begin
: :   aValue:=1;
: : end;
: : 
: : procedure TForm1.ChangeIntVAR(var avalue:integer);
: : begin
: :   aValue:=2;
: : end;
: : 
: : procedure TForm1.ChangeEdit(aEdit:TEdit);
: : begin
: :   aEdit.Text := '1';
: : end;
: : 
: : procedure TForm1.CHangeEditVAR(var aEdit.TEdit);
: : begin
: :   aEdit.Text := '2';
: : end;
: : 

: :
: Objects are passed as pointers. This means that the aEdit is a pointer, which holds several other values. If you change a property then the object pointer isn't changed, only a part of the memory to which it points. Since that memory part is located in the heap instead of the procedure stack, it will be changed globally.
: In case of simple types (integers, floats, strings, etc), the value is copied from the heap into the procedure stack. If that value is changed, the heap copy isn't updated, unless the parameter was variable.
:
Thank you. I appreciate the reply & info.
dve83



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.