0

Link to the class :

https://stackoverflow.com/a/4543089/6591306

My question is simple, the usage of the class is as so :

The getter is getting () => variable, and the setter is getting z => { variable = z; }

In order to call the function i call it like this :

ref<int> tempx;
int tempy = 5;
tempx=new Ref<int>(() => tempy, z => { tempy = z; });
tempy = 6;//tempx.Value becomes 6
tempx.Value = 7;//tempy becomes 7

I want to achieve that in order to call the class i'll do it :

tempx=new Ref<int>(tempy);

without writing the actions, so that it won't take alot of lines of code, and the actions will be saved in the class, and whenever i call it they'll be performed automatically.

I dont know how to achieve it, therefore i'm asking here.

matan justme
  • 371
  • 3
  • 15
  • 1
    That is completely impossible. There is a reason that they use that syntax. – SLaks Jun 05 '17 at 20:55
  • However, look at C# 7 `ref` returns. – SLaks Jun 05 '17 at 20:55
  • @SLaks That still wouldn't let you do this. – Servy Jun 05 '17 at 20:56
  • The magic here is that the lambdas are creating a captured closure over your variable. So they're required in order for this to work. – juharr Jun 05 '17 at 21:02
  • @juharr using a ref I obviously can't, maybe there's a way to create 2 captured closures? 1 that is a ref of the variable and a second of the first captured closure with actions inserted (like a delayed entry)? – matan justme Jun 05 '17 at 21:25

2 Answers2

0

You can't. There's no way to pass in an int and end up with the ability to get or set it at some arbitrary point in time in the future; you need to create lambdas in order to be able to do that.

Servy
  • 202,030
  • 26
  • 332
  • 449
  • can you check my last comment and i'll quote : "using a ref I obviously can't, maybe there's a way to create 2 captured closures? 1 that is a ref of the variable and a second of the first captured closure with actions inserted (like a delayed entry)? " – matan justme Jun 06 '17 at 20:53
  • @matanjustme No, that's not possible. Feel free to write the code yourself to see, if you don't believe me. – Servy Jun 06 '17 at 21:15
  • i see... it makes sense that it's not possible, but still unfortunate. ty for the answer and for the time, appreciating it. – matan justme Jun 06 '17 at 21:46
-1

I know this is an old question, but this extension method works for me:

public static Ref<T> GetRef<T>(this T obj)
{
    return new Ref<T>(()=>obj,x=>obj=x);
}

You use it like this:

Ref<targetType> yourRefName=target.GetRef<targetType>();

or in your case:

tempx=tempy.GetRef<int>();

Edit: This only works for classes, not value types like ints, so doesn't help with this specific question

NEPTTUNE
  • 58
  • 4
  • The code *compiles*, but no, it doesn't work. The parameter to your method isn't passed by reference, and so changes to the parameter don't affect the variable in the calling code and vice versa. – Servy Feb 09 '23 at 19:21
  • I've literally tried it and the value was changed on the variable, so I don't know why it didn't work. – NEPTTUNE Feb 10 '23 at 10:30
  • Sounds like you're using a mutable reference type, and you didn't mutate the variable, you just mutated a field that is being referenced. The exact same thing would happen if you didn't use this method/type at all. Use the example that the demonstration from the question uses (namely using an integer) to ensure you're changing the variable, in question and not a something it references. – Servy Feb 10 '23 at 12:54
  • Ah yes, I see now, I was using a class to test - changing a field of the class using the Ref<> and then outputting the field's value directly from the class, which was changed; having just tested with an int it didn't work. – NEPTTUNE Feb 10 '23 at 13:03