0
//save the current origin value        
Employee temp = Origin;
// do some staff with origin and it changes the origin value.

//restore origin to the temp
Origin = temp;
//here i expected the origin value restore to the temp but it doesn't.

temp value changes with origin value. in other word both temp and origin has the same values!!!

I want to save Origin value and the restore it, but if origin changes the temp changes too. Its like compiler doesn't initiate temp.

I guess its a real fundamental developing knowledge but i couldn't find the answer. so sorry about that.

UPDATE so far i tried these but non of them worked:

var temp = Origin; 
object tmp = temp; 
Origin = (Employee)tmp;

var temp = new Employee(); 
temp = Origin;
Origin = temp;

finally as dear @erikscandola said I copy all properties one by one in to the temp object and its worked. but that was very hard(assuming the Employee is real big object). is there a better way to do such a thing? it really seems easy.

Mohammad
  • 2,724
  • 6
  • 29
  • 55
  • 3
    Make sure you understand the difference between [value types and reference types](https://msdn.microsoft.com/en-us/library/t63sy5hs.aspx) and you will understand what is wrong with your code. (Hint - it seems you are using reference types) – dotnetom Jan 03 '16 at 08:08
  • Post lacks [MCVE], so can't really be answered, but you likely find explanation in http://stackoverflow.com/questions/5057267/what-is-the-difference-between-a-reference-type-and-value-type-in-c. – Alexei Levenkov Jan 03 '16 at 08:08
  • @dotnetom thank you so much. i get it now. its look like both temp and Orgin has the same memory. but how can solve my problem? can i create temp value with its own memory? – Mohammad Jan 03 '16 at 08:12

3 Answers3

4

Both temp and Origin are reference types, temp and Origin are pointing to the same memory location on the heap and therefore they have the same value. To avoid this create new Employee as:

var temp = new Employee();

This will create a new memory location on the heap and thus having its own different value.

Idos
  • 15,053
  • 14
  • 60
  • 75
Sarveshwar
  • 125
  • 2
  • 15
2

It is the normal behavior of C#. When you assign an object to another object you assign the same instance. So temp and Origin have the same pointer. In order to assign Origin to temp you need to do this in the constructor:

public Employee(Employee e){
    // copy all property values
}

Then you call the constructor:

Employee temp = new Employee(Origin);

Now you can do what you want with Origin without changing the value of temp.

Idos
  • 15,053
  • 14
  • 60
  • 75
erikscandola
  • 2,854
  • 2
  • 17
  • 24
  • Thanks. but its sort of impossible!! employee has so many properties. and also i don't have access to the class. – Mohammad Jan 03 '16 at 08:16
  • You can create a method to copu all properties. Initializate a new Employee `Employee temp = new Employee();` then call method to copy all `CopyEmployeed(temp, Origin);` – erikscandola Jan 03 '16 at 08:19
0

Class in .Net is Reference Type and when change on of them thay are change. you can use an Object Temp like this :

Employee temp = Origin;

Object tmp = temp;

and when you want use , you should cast befor use. enter image description here

Uthman Rahimi
  • 708
  • 5
  • 22