Posted By: ploe | May 22nd, 2008 @ 7:51 AM
page 1 of 1
Comments: 14 | Views: 1835

I pass in an object to a method. This method modifies some properties of the object. After the method returns the changes it made to the object seem to have disappeared as if the object passed in was passed by value rather than reference.

Is there any instance where C# would do such a thing?

This is how I would like write my code:
ResetStopTimes(stops, loadTimes);

private static void ResetStopTimes(Stop[] stops, Dictionary<int, TimeSpan>) { ... }

I need to write my code like this in order to get the changes out of the function:
stops = ResetStopTimes(stops, ref loadTimes);

private static Stop[] ResetStopTimes(Stop[] stops, ref Dictionary<int, TimeSpan>) { ... }

It's wierd as most places I don't need this hack. It behaves like I would expect it. Anyone know what's going on here?

TommyCarlier
TommyCarlier
I want my scalps!
Inside ResetStopTimes, did you set the parameter to "new Dictionary<int, TimeSpan>()" somewhere?
Richard.Hein
Richard.Hein
... my guitar gently weeps ...

I think you're going to have to show the rest of the relevant code.

ploe wrote:


I pass in an object to a method. This method modifies some properties of the object. After the method returns the changes it made to the object seem to have disappeared as if the object passed in was passed by value rather than reference.



C# always passes by value, unless you specify otherwise.

ploe wrote:


Is there any instance where C# would do such a thing?

This is how I would like write my code:
ResetStopTimes(stops, loadTimes);

private static void ResetStopTimes(Stop[] stops, Dictionary<int, TimeSpan>) { ... }

I need to write my code like this in order to get the changes out of the function:
stops = ResetStopTimes(stops, ref loadTimes);

private static Stop[] ResetStopTimes(Stop[] stops, ref Dictionary<int, TimeSpan>) { ... }

It's wierd as most places I don't need this hack. It behaves like I would expect it. Anyone know what's going on here?



You need to provide more code for us to figure out what's wrong here.
Make a copy of the object
To provide any further help we really do need what is happening in ResetStopTimes.

So long as you're only changing properties/elements inside each of the collections changes should be seen outside the method. Both Stop[] and Dictionary are class types, so the only change that shouldn't be populated up is if you ever do something that creates or otherwise modifies the actual reference to the object, since that reference is a copy and not a reference to the reference the changes won't be picked up because the changes will happen on a different object.

Note that new isn't the only way that a reference type would replace it's reference with a reference to a new object. Something like Clone, Resize, and other things might change the reference to the object.

It seems like since adding the ref fixes it that somewhere you're causing the reference to be changed to a new object, since passing a class by reference effectively passes a reference to the reference allowing you to change the reference.
wkempf wrote:

ploe wrote:

I pass in an object to a method. This method modifies some properties of the object. After the method returns the changes it made to the object seem to have disappeared as if the object passed in was passed by value rather than reference.



C# always passes by value, unless you specify otherwise.
yep, but he said he doesn't modify the value of the reference, I don't understand why doesn't people learn how to debug their code... life would be so easier, no endless threads like this...
Matthew van Eerde
Matthew van Eerde
AKA Maurits
wkempf wrote:
C# always passes by value, unless you specify otherwise.


Sort of.  Value types are passed by value unless you specify otherwise.  Reference types cannot be passed by value - they're always passed by reference.  You can specify "out" to force the called function to set the parameter.

Arrays are reference types so I have no idea why the code in the original post doesn't work.  Maybe it's not doing what we think it's doing?
Matthew van Eerde wrote:
 Reference types cannot be passed by value - they're always passed by reference.
actually, reference types are *passed by value* meaning that the value of the reference is passed, that's why you cannot change the reference
littleguru
littleguru
<3 Seattle
Ion Todirel wrote:

Matthew van Eerde wrote:  Reference types cannot be passed by value - they're always passed by reference.
actually, reference types are *passed by value* meaning that the value of the reference is passed, that's why you cannot change the reference


Oh boy... this remembers me of the other discussion that we had like 1-2 years ago... it's true: reference types are also passed by value. if you want ot pass them by reference you need to use the ref keyword. The deal here is that the reference (pointer) to the reference type is passed and that's then passed by value.
Minh
Minh
WOOH! WOOH!
ploe wrote:



What ended up fixing the problem was restarting VS. I have no idea what caused this... Expressionless

Yeah, VS drove me mad once, too.
Minh wrote:

ploe wrote:


What ended up fixing the problem was restarting VS. I have no idea what caused this... Expressionless

Yeah, VS drove me mad once, too.
but that wasn't VS but the C# compiler itself, isnt?
Minh
Minh
WOOH! WOOH!
Ion Todirel wrote:

Minh wrote:
ploe wrote:


What ended up fixing the problem was restarting VS. I have no idea what caused this... Expressionless

Yeah, VS drove me mad once, too.
but that wasn't VS but the C# compiler itself, isnt?

Aahh. So, VS is mis-reporting the values. I guess VS is also a collection of lines of code... and subject to bugs, too.
page 1 of 1
Comments: 14 | Views: 1835
Microsoft Communities