Click to See Complete Forum and Search --> : Method with parameter who changes the initial member
RebornBear
January 2nd, 2006, 07:32 PM
Hi,
I declared a method with parameter as :
private void Method(object pObject)
{
pObject.Property = 5;
[...]
}
but when I call this method as:
private void btn1_Click(object sender, System.EventArgs e)
{
Method(m_object);
}
the property of m_object is changed, even if I never modified it directly.
My question : Is it normal that the parameter changes the initial member? If it is, how can I avoid it?
I hope I made myself clear enough!
Thanks!
wildfrog
January 2nd, 2006, 07:40 PM
Is it normal that the parameter changes the initial member?I'm not sure you're making yourself clear enough... what do you mean by 'changes the initial member'?
If it is, how can I avoid it?If a function call is doing something you don't want to happen, then either don't call that function or rewrite/create a new function tht does what you want it to do.
- petter
RebornBear
January 3rd, 2006, 12:23 AM
The problem is that the property of "m_object" is changed even though I called the change on the property of "pObject".
I'm asking if it is normal that both properties of pObject and m_object are changed, considering my code.
I'm sorry if I am not clear, but english is my second langage and I didn't learn the terminology of C# in english. I still have problem with the terminology in french, so just imagine my confusion. hehe
Thanks!
mmetzger
January 3rd, 2006, 12:31 AM
Based on your code, this is perfectly normal. Chances are you are running into an issue of scope. I assume due to your question you have another object called pObject. In this instance, the local pObject for the method takes precedence over any other. So when you pass in mObject, the method receives a local version of it (pObject).
To solve this, You can call the local parameter something else which won't collide with the name, try accessing the literal object by it's full name (a la, this.pObject, etc.) or simply create a method that doesn't require a parameter to change the object you wish.
boudino
January 3rd, 2006, 02:51 AM
I think that all what RebornBear is missing is mechanism of passing parameters. The pObject is just a formal parameter. It is a placeholder in the method, which is set with an object in runtime. If you call Method(m_object), reference to m_object is passed to Method trought the pObject parametr. m_objects and oObjects now "points" to the same object. Than if you change something in pObject, it is definitely changed in m_object (In fact, pObject and m_object are both only aliases to some object somewhere in managed heap).
Don't forget that you are working with objects. Slightly different situation is when you have been working with structures, which are copied if passed as parameter. If the pObject would be a structure, you realy can expect that chaning pObject won't affect m_object. But I would strongly recommend you to avoid structures and use only objects.
jhammer
January 3rd, 2006, 08:16 AM
Have a look here:
http://www.yoda.arachsys.com/csharp/parameters.html
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.