Assignment of value to variable [closed]

0

I have two global variables, which we'll call A and B , when I make the following assignment:

A:= 5;
B:= A;

Whenever I change the value of A , the value of B also changes. If I do

A:= 10

B becomes 10 as well. How can I change the value of A without changing the value of B ?

    
asked by anonymous 24.10.2018 / 23:09

2 answers

2

In Delphi code runs line by line soon if you have the following code:

A := 5;
B := A;
showmessage('A=' + IntToStr(A) + '; B=' + IntToStr(B));
  

Message: A = 5; B = 5

You are assigning the same value of A as B variable. For B to have a value other than A just remove the assignment of the same " B := A; " code example:

A := 10;
//B := A;
B := 62;
showmessage('A=' + IntToStr(A) + '; B=' + IntToStr(B));
  

Message: A = 10; B = 62

Not being the most correct option (depending on the cases) you can still reassign the value of the B variable, for example:

A := 10;
B := A;
showmessage('A=' + IntToStr(A) + '; B=' + IntToStr(B));
  

Message: A = 10; B = 10

B := 62;
showmessage('A=' + IntToStr(A) + '; B=' + IntToStr(B));
  

Message: A = 10; B = 62

There is also a last option that is to assign a new value to A as follows, for example:

A := 5;
B := A;
A := 10;
showmessage('A=' + IntToStr(A) + '; B=' + IntToStr(B));
  

Message: A = 10; B = 5

    
25.10.2018 / 12:21
-3

You are declaring that B is equal to A , so when you change A .

A:= 5;
B:= A; //Aqui que você declara que B é igual a A

One thing you can do is declare B as 5 and change the value of A

A:=5;
B:=5;
A:=10;

So, B is not set to A , so it is not changed.

    
25.10.2018 / 01:41