Updating an object inside / outside a method
Okay,
Maybe this is by C # design, or maybe I'm wrong.
Please take a look at the code I have below. I did this where you can copy and paste it into a console app and run it to see what I'm talking about.
using System;
namespace ConsoleTest
{
class Program
{
static void Main()
{
var o1 = new oObject
{
Name = "Before Method"
};
var o1b = new oObjectBad
{
Name = "Before Method"
};
Console.WriteLine("Object Bad 1 Before: " + o1b.Name);
oObjectBad o2b = GetNewObjectBad(o1b);
Console.WriteLine("Object Bad 1 After: " + o1b.Name);
Console.WriteLine("Object Bad 2 After: " + o2b.Name);
Console.WriteLine(string.Empty);
Console.WriteLine("Object 1 Before: " + o1.Name);
oObject o2 = GetNewObject(o1);
Console.WriteLine("Object 1 After: " + o1.Name);
Console.WriteLine("Object 2 After: " + o2.Name);
Console.ReadLine();
}
public static oObject GetNewObject(oObject o)
{
oObject newObject = new oObject(o);
newObject.Name = "Changed in Method";
return newObject;
}
public static oObjectBad GetNewObjectBad(oObjectBad o)
{
o.Name = "Changed in Method";
return o;
}
}
class oObject
{
public oObject()
{
}
public oObject(oObject o)
{
Name = o.Name;
}
public string Name { get; set; }
}
class oObjectBad
{
public string Name { get; set; }
}
}
Now here's what I don't understand.
When I pass an object to a method and update the object's "Name" property. Then it updates the original object that is outside the method.
The way around this was to create another object (as shown in the code) to make a clone of the object, then update the clone, and then return the clone.
So the question is this: Is there an easier way to pass an object and update the properties of that object so that I can have something like this without updating it to the original object.
using System;
namespace ConsoleTest
{
class Program
{
static void Main()
{
var o1 = new oObject
{
Name = "Before Method"
};
Console.WriteLine("Object 1 Before: " + o1.Name);
oObject o2 = GetNewObjectBad(o1);
Console.WriteLine("Object 1 After: " + o1.Name);
Console.WriteLine("Object 2 (New Object) After: " + o2.Name);
Console.ReadLine();
}
public static oObject GetNewObjectBad(oObject o)
{
o.Name = "Changed in Method";
return o;
}
}
class oObject
{
public string Name { get; set; }
}
}
If this is confusing, let me know and I will see if I can explain better.
EDIT: The reason I am doing this is because I need to clone an object in the database and the cloned object will have to change a couple of properties. Then I will use the ID of the original object and the new object to process the data differently.
Thanks!
a source to share
I would spend some time learning pass by reference vs. pass by value and then looked at why you need to change the name in the local context but not in the original. I have a suspicion that you may have some design flaws. I would revisit the problem and see if a solution can be developed with a different approach.
a source to share
Why do you want to change the object in the method and not have this data reflected in the calling method? One thing you can do is implement change tracking code and give the class a method that returns the original data object.
Like others have suggested, this is a value type versus a reference type issue. If you really want the object not to change, you can change your type to structure. I am not saying this is the answer, although I do not know your class structure to make an informed opinion on the matter.
a source to share
In .NET, all objects are reference types unless they descend from ValueType. Typically, you don't want to change the object instance variables in any way. You have a lot more control if you make the instance variables private and then add methods to manipulate the private data.
http://msdn.microsoft.com/en-us/library/s1ax56ch.aspx List of ValueType decients.
a source to share