Sign in to follow this  

C# basics question

This topic is 3663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi.. I'm trying to write a system that will let me store a reference to an object and when I change that reference it will update the original object too. It boils down to this: object originalInt = 5; object refInt = originalInt; refInt = 6; This will set refInt to 6 and leave the originalInt at 5. How can I set the value of refInt and make it also change the OriginalInt?

Share this post


Link to post
Share on other sites
integers are value-types, all value types are copied instead of referenced.
If you want an integer to behave like a non value-type you'll have to create a class which has an integer property.
Classes are referenced, not copied.

class Bla
{
public int myInt;
}

Bla originalInt = new Bla();
originalInt.myInt = 5;
Bla refInt = originalInt;
refInt.myInt = 6;

now originalInt.myInt is 6

Share this post


Link to post
Share on other sites
Thanks for the reply.. I read this article too just now which seems to confirm what you've said:

http://www.yoda.arachsys.com/csharp/parameters.html

It does seem particularly nasty though that I have to write classes to wrap the value-types I want to store. I'm new to C Sharp but it seems frustrating that the debugger can do with such ease what I cannot. :p

If I expand the object the debugger knows the type of the object (which I can get with .GetType() etc). Then if I expand the System.Int32 within the object I can see a few member variables, including m_value, MaxValue and MinValue.

Now m_value is the key here, if I change that then it will change the value for all objects that hold the same reference. But m_value is a private member and so I cannot change it. Any ideas? Or am I speeding dangerously into forbidden unsafe code? Wheeeeee!

Share this post


Link to post
Share on other sites
Quote:
Original post by nukomod
Now m_value is the key here, if I change that then it will change the value for all objects that hold the same reference.


Except that System.Int32 is a structure, not a class, so it's handled by value as well and thus doesn't solve your problem.

Quote:
Any ideas? Or am I speeding dangerously into forbidden unsafe code? Wheeeeee!


I suspect that you don't need to do this kind of manipulation in the first place. I can't even remember the last time I had to do something like this.

Share this post


Link to post
Share on other sites
Perhaps this is not what you were looking for, but have you already considered to use the 'ref' kwyword to pass the values by reference? I suppose that this may be a possibility only if the cases where you need that behaviour are few...

Share this post


Link to post
Share on other sites

This topic is 3663 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this