Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Params by value / reference

dve83dve83 Posts: 30Member
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.

[code]
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;
[/code]

Comments

  • zibadianzibadian Posts: 6,349Member
    : 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.
    :
    : [code]
    : 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;
    : [/code]
    :
    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.
  • dve83dve83 Posts: 30Member
    : : 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.
    : :
    : : [code]
    : : 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;
    : : [/code]
    : :
    : 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
Sign In or Register to comment.