You are on page 1of 26

VALUE & REFERENCE

IF Ubaya (Infor + SIB + MM + ITDD)

1
INTRODUCTION
• The objects (variables) in C# can be categorized
into two types:
 a value type object/variable and
 a reference type object/variable

• A value type variable is used to store the data itself


• A reference type variable is used to store the
address of the data location. By going to the
address, we will be able to get the data.
• The address, in computer, is just a number from 0 to
the size of your computer memory, representing the
location of the data
2
Value vs Reference: Illustration
Cup = 100 ml coffee Table.Cup = 100 ml coffee
(read: fill the cup) (read: go to the table, and fill the cup)

IN PROGRAMMING
int mark = 100; labelMark.Text = “100”;
3
Struct: Value Data Type
• Variable that is created from a structure (struct)
such as int, double, long, bool, DateTime, etc. will
become a value type variable. See VS Intellisense.
• Note: “int” is actually the same as “Int32” structure

4
Class: Reference Data Type
• Variable that is created from a class such as Label,
TextBox, PictureBox, ListBox, List , Random, etc.
will become a reference type variable.

5
Class: Reference Data Type
• Remember, a reference variable is not the place
where you will store the value
• So doing this will give you an error:
TextBox textBoxName = “Budi”; 

• You have to create the storage with new command


TextBox textBoxName = new TextBox();
• Then you can use the reference to access the
storage (properties)
textBoxName.Text = “Budi”;
6
Value and Reference Var Inside Memory
Memory Memory
Value type variable Address Content

int total = 100; 8000 100


(total)
(the value 100 is stored
in a memory at address
8800 [for example])

Memory Memory
Reference type variable Address Content
Label myLabel = new Label(); 9000 25000
(myLabel)
myLabel.Text = “Hello”;
: :
(The number 25000 [for ex.]
: :
in memory address 9000 show
25000 “Hello”
that the data is at address 25000) 7
Copying the value variable
See the difference between value and reference type
variable when we copy the variable
int total = 100; Memory Memory
Address Content
int total2 = total;
8000 100
The value 100 in “total” is taken (total)
And be copied to “total2” 8004 100
(total2)
In this case, you have Two data
that are stored in two variables
“total” and “total2”

8
Copying the reference variable
Now, what about the reference type variable?
Label myLabel = new Label();
myLabel.Text = “Hello”; Memory Memory
Address Content
Label myLabel2 = myLabel;
The value 25000 in “myLabel” is 9000 25000
(myLabel)
taken and be copied to “myLabel2”
9004 explained in
(myLabel2) classroom
Remember that 25000 actually
: :
IS NOT the data that you want to
copy. In this case, you have : :
ONE data (“Hello”) but 2 variables that :
:
refer to that memory location 25000
25000 “Hello”
Maybe this is NOT what you 9
want when you copy the data
One Data, Two Ref. var. : Problem
Having two variables that refer to the same data will
enable you to change the data, from any variables.
What’s the output? (MB = MessageBox)
Memory Memory
myLabel.Text = “Hello”; Address Content
myLabel2.Text = “Darling”; 9000 25000
MB.Show(myLabel.Text); (myLabel)
MB.Show(myLabel2.Text); 9004 25000
(myLabel2)
:
myLabel.Text = “Good”; :
MB.Show(myLabel.Text); : :
myLabel2.Text = “Bad”;
MB.Show(myLabel2.Text); :
MB.Show(myLabel.Text); 25000 ???
10
Deep copying reference var.
Steps to copy the CONTENT of the memory referred
by a reference variable:
• Create new location for the data (using new),
• Copy the property of the data one by one.
Copying the properties one by one is called Deep
Memory Memory
Copy. Address Content
Label myLabel3 = new Label(); 9000 25000
(myLabel)
myLabel3.Text = myLabel.Text;
9004 25000
myLabel3.Left = myLabel.Left; (myLabel)
..Etc (for all properties) 9008 explained in
See the illustration  (myLabel3) classroom
What is the output of these codes? :
25000 “Hello”
26000 ???
11
PASSING BY VALUE and
PASSING BY REFERENCE

12
Passing by Value
• By default, C# will perform passing by value to send
(copy) the value from the argument to the parameter
• In addition to pass by value, C# allow us to do a
passing by reference.
• Because there are two kinds of variables (value and
reference) and two types of passing (passing by
value/reference) then we have 4 possibilities:
1. Passing by value for the value type variable
2. Passing by value for the reference type variable
3. Passing by reference for the value type variable
4. Passing by reference for the reference type variable
• In all of our previous methods, we used pass by value
for the value type variable. 13
1. Pass by Value for the Value Type variable
• Let’s see the content of the memory for this operation.
Assume the value in num and denom is 9 and 12.
Memory Memory
private void buttonSimplify_Click(...) Address Content
{
8000 9
int num = int.Parse(textBoxNum.Text); (num)
int denom = int.Parse(textBoxDenom.Text);
8004 12
int theGcd = FindGcd(num, denom); (denom)
:
8008 9
} (num1)
8012 12
private int FindGcd(int num1, int num2) (num2)
{
8016 ???
int gcd = 1; (gcd)
:
8020 ???
return (gcd); (theGcd)
}
14
2. Pass by Value for the ref. type variable
• Example of reference type variable: List
• Let’s pass the list by value
private void buttonIncreaseBy10_Click(...) Memory Memory
{ Address Content
List<int> listOfMark = new List<int>(); 9500 30000
listOfMark.Add(70); (listOfMark)
listOfMark.Add(80); 9504 30000
AddTen(listOfMark); (myList)
: 9508 0
} (i)
:
private void AddTen(List<int> myList)
{
30000 70
for (int i=0; i<myList.Count; i++)
{ 30004 80
myList[i] += 10;
} 15
}
3. Pass by reference for the value type var.
• We want the value type variable can have new value
• Example: swap two numbers in parameters.
• Let’s try to do it by Pass by value (draw the memory)
private void buttonChange_Click(...)
{
int firstNum = int.Parse(textBoxFirst.Text);
int secondNum = int.Parse(textBoxSecond.Text);
SwapNumber(firstNum, secondNum);

textBoxFirst.Text = firstNum.ToString();
textBoxSecond.Text = secondNum.ToString();
}
private void SwapNumber(int num1, int num2)
{
int helper = num1; Can the program swab the
num1 = num2; value in the parameters? 16
num2 = helper;
3. Use Pass by Reference
• Give ‘ref’ keyword in the parameter and the argument
(the same keyword, but different meaning)

Memory Memory
private void buttonChange_Click(...) Address Content
{
int firstNum = int.Parse(textBox.. 8000 10
int secondNum = int.Parse(textBo.. (firstNum)
8004 99
(secondNum)

For example, the value


taken from the text
boxes are: 10 and 99

17
Let’s see the memory for the method parameter
• ref keyword in the parameter means that the variabel
will accept address, and not ordinary number

Memory Memory
private void buttonChange_Click(...) Address Content
{
int firstNum = int.Parse(textBox.. 8000 10
(firstNum)
int secondNum = int.Parse(textBo..
8004 99
: (secondNum)
} 8008
(ref num1)
private void SwapNumber(ref int num1, 8012
ref int num2) (ref num2)
{
:
}
18
Calling the method with ref in the argument
• ref keyword in the argument means that we want to
send the address of the variable and not its content

Memory Memory
private void buttonChange_Click(...) Address Content
{
int firstNum = int.Parse(textBox.. 8000 10
(firstNum)
int secondNum = int.Parse(textBo..
8004 99
(secondNum)
SwapNumber(ref firstNum, ref secondNum);8008 8000
(ref num1)
: 8012 8004
(ref num2)
}

private void SwapNumber(ref int num1,


ref int num2)
{ 19
:
Using the reference variable
• The code inside the method will access the data in
the referenced memory

private void SwapNumber(ref int num1, Memory Memory


ref int num2) Address Content
{
// Note: you can think that: 8000 10
(firstNum)
// num1 is firstNum, and
// num2 is secondNum 8004 99
(secondNum)
8008 8000
int helper = num1; (ref num1)
num1 = num2;
8012 8004
num2 = helper; (ref num2)
}
8014 ???
(helper)

20
AFTER the method is called
• Because firstNum and secondNum are accessed
directly, then the modification will stay
After the method is called
private void buttonChange_Click(...)
{
int firstNum = int.Parse(textBox.. Memory Memory
int secondNum = int.Parse(textBo.. Address Content
SwapNumber(ref firstNum, ref secondNum);
8000 10 99
The value of firstNum and secondNum in (firstNum)
here, can be seen in the diagram 8004 99 10
(secondNum)
}
private void SwapNumber(ref int num1, 8008
(ref num1)
ref int num2)
{ 8012
int helper = num1; (ref num2)
num1 = num2; 8014
num2 = helper; (helper)
} 21
Passing by Reference
• As you see in the previous slide, passing by
reference can be used to get the results from
the parameters.
• If you want to get more than one result from a
method  use pass by reference
• If you want to get ONLY one result from a
method  use return keyword

22
Two keywords for Passing by Reference
• There are two keywords that you can use to
perform passing by reference.
• The keywords are: ref and out
• The difference.
Ref is used to modify the value in the argument.
It means that the argument should already have
an initial value before it is send to the parameter
Out is used to give a value to the argument. The
argument does not need to have initial value and
later on, the method must assign any value to the
parameter. 23
EXERCISE
• A curve
y = ax2 + bx + c,
can have 3
possibility to cross
the horizontal axis:
• It does not cross it at
all (the black curve)
• It crosses at two
points (the red one)
• It crosses (touches)
at one point (the
blue one)
24
EXERCISE
• A formula to find the x coordinates where the intersection with
the X axis occurs is called the ABC formula, and it is defined
as:

• Create a program to help the user to find the value of


X1 and X2, by entering the value of a, b, c.
• You should create a method, that will accept the
value of a, b, and c, and produce the value of X1
and X2 in the method parameter. 25
EXERCISE
• Your method should also return a value of 0, 1, and 2 that
determine the number of intersection with x axis (0: no
intersection, 1: one intersection, 2: two intersection). Use the
method output.
• Check the value of “b2 – 4ac” before you do the Square root
calculation.
 If this value is negative (less than 0)  No intersection.
No need to do the square root. Return 0.
 If this value is 0  there is One intersection. Do the
square root (and other things). Return 1.
 If this value is positive (more than 0)  Do the square root
(and other things). Return 2.
26

You might also like