Sign in to follow this  

object references

This topic is 3949 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

Hello, I have a question about object references, and I am on vacation without a compiler to check it, but still programming (notepad ftw.) I'm using C#. There are four classes, Base, Sub1 : Base, Sub2 : Base and a main class that has a Hashtable with keys of strings and values of Base. The Sub1 class will sometimes call "MainClass.foo(this);" where foo has a parameter of Base. Now, the main class has a reference to said Sub1 in its Hashtable, and also now passed as a parameter. These reference the same object. What if foo has code: paramBase = new Sub2(); Will this change the reference in the Hashtable to now point to the same thing? Part of me wants to say yes, yet that command merely changes paramBase to point to something else; wouldn't the Sub1 still exist in memory, still referenced in the Hashtable? If indeed the Hashtable's reference is also changed to the new Sub2, then the Sub1 is effectively gone, waiting for the garbage collector, correct?

Share this post


Link to post
Share on other sites
Moved to For Beginners.

Share this post


Link to post
Share on other sites
I think I understand your problem.

If you change one reference, you don't change any other reference (even if they originally pointed to the same object). So if you change the reference in the class, but not the hashtable, the hashtable reference will still point to the old instance.

Share this post


Link to post
Share on other sites
I'm quite confident that it will only change the local reference. The hashtable should continue pointing to what it had been previously. What you need is some form of double reference. In C++, what I'm most familiar with, you'd have a whole bunch of options, an iterator to a pointer, a pointer to a pointer, reference to a pointer, iterator to a smart pointer, et cetera. I'm not sure what the equivalent in C# would be. I'm actually rather surprised I haven't had to figure this out for myself yet. I suppose you could just create a new class, and all the class did was contain a reference to another class. (You could even make it a generic class.) And then you store this reference class in the hashtable, and this reference class stores a reference to Base. But that feels like it would just be reproducing some effect that should already exist.

Share this post


Link to post
Share on other sites

This topic is 3949 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