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
  • 353
  • 2
  • 12
  • 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

1 Answers1

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
  • 197,813
  • 25
  • 319
  • 428
  • 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