Sign in to follow this  
Telastyn

[.net] Handles/double pointers in .NET?

Recommended Posts

I found a need this evening for handles or double pointers depending on what terminology you want to use. That is, I want to be able to store a ref parameter as-is so that the stored reference changes as the original reference is reassigned. The closest I can find is storing a delegate which is explicitly passed ()=> target. (edit: that is, an anonymous delegate that does nothing but returns the reference) Icky. Isn't there an easier way?

Share this post


Link to post
Share on other sites
I'm not entire sure what your trying to do but if I understand you the best solution I can think of is a intermediate object that can both deal with any problems with the reference it holds and protect itself from external tampering using propertys or get/set methods

Share this post


Link to post
Share on other sites
The intermediate object will get a copy of the reference. Any later changes won't be picked up.

Expected behavior:



string foo = "foo";
ref<string> bar = foo;
Console.WriteLine(bar); // foo
foo = "bar";
Console.WriteLine(bar); // bar



Which can be implemented:


public class Ref<T>{
public delegate T RefType();
public readonly RefType RefHandle;
public static implicit operator T(Ref<T> rhs){return(rhs.RefHandle());}
public Ref(RefType incoming){ RefHandle = incoming; }
}

string foo = "foo";
Ref<string> bar = new Ref<string>(()=>foo);
Console.WriteLine(bar); // foo
foo = "bar";
Console.WriteLine(baR); // bar



But is... less than ideal.

Share this post


Link to post
Share on other sites
The implementation of your Ref class may be ugly and hackish, but the way it actually gets used seems quite simple and elegant. Why not use it? Do you have performance concerns, or do you just dislike it's hackish appearance?

Share this post


Link to post
Share on other sites
Quote:
Original post by gharen2
The implementation of your Ref class may be ugly and hackish, but the way it actually gets used seems quite simple and elegant. Why not use it? Do you have performance concerns, or do you just dislike it's hackish appearance?


Ideally I'd like to pass the reference into the constructor (perhaps with the ref qualifier). It's not something I wouldn't use for my needs, but it seems like something that should already exist and I don't know about it. *shrug*

Share this post


Link to post
Share on other sites
The original need was while doing some dabbling with a traits/mixin system. In general, anything like that (the decorator pattern jumps to mind) where the actual reference is replaced with a new (albeit functionally similar) one causes all sorts of problems when you want reference semantics but also to do runtime type composition. It also has applications with data binding and aliasing.

Share this post


Link to post
Share on other sites

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