-1

In my code I have a new item property setter in the form of lambda expression. It's as simple as

( x ) => x.Property = y

I wrote it to an Action<T> variable and I've been using it when needed.

The problem is, when I was "printing" object that was sent to that lambda as an input to gui, I found out that the property remains as null.

I've tried debugging it and while in that lambda expression, I can see that the object's property is being set to desired value, but the moment I step out of lambda, Property is back to null. For some reason, it's as if lambda operates on a copy of this object instead of it's reference?? I've tried rewriting it to delegate void ActionRef<T>( ref T item ) (which I came up with after some reading of SO questions) but this worked exactly the same way.

Finally, I've tried with Func<T,T> and somehow again, it returns an object with property still set to null. I've even tried setting some default type properties (string, int, DateTime) just to test if there is something wrong with objects of my custom classes, but no, those also weren't properly set after leaving the lambda.

I seriously have no clue what can be the reason for that. I even wrote a simple test app that does similar stuff and it works just fine. It is first time I have problems with lambdas.

I doubt that code samples are really needed here, but in case someone needs that:

class A {
    A ( Action<T> setter ) {
        newItemPropertySetter = setter;
    }
    void InsertNewItem() {
        …
        T item = new T();
        if ( newItemPropertySetter != null )
            newItemPropertySetter( item );
        …
    }
}

class B {
    …
    … = new A( ( x ) => x.Property = y );
    …
}

Finally, one last thing that just came to me is something that might be somehow related to this strange issue - T classes are my ORM classes used with nHibernate. This still does not help me understand why property I've set to some value suddenly is unset back to null / default.


Edit: After quite a bit of debugging, I've found out that in the end some other piece of code was immediately overwriting that property with default value. Sorry for the trouble.

Soul Reaver
  • 2,012
  • 3
  • 37
  • 52
  • 1
    and how do you examine the `Item` isn't updated? I suppose as of your code you create a new `Item` within `InsertNewItem`, which is destroyed when the your app leaves that function. – MakePeaceGreatAgain Apr 08 '22 at 12:35
  • Show us the code that is missing. MikolajR probably has guessed it right; `y` is not being closed over. – Robert Harvey Apr 08 '22 at 12:38
  • I had to edit your sample code a bit to get it to compile (which means that it's not a [mcve] -- the "complete" bit means that we must be able to run it and see your issue). However, it works fine: https://dotnetfiddle.net/qyozLT – canton7 Apr 08 '22 at 12:43
  • Are you dealing with structs? if yes, that is to be expected - see here: https://dotnetfiddle.net/craT52 – Rand Random Apr 08 '22 at 12:43
  • @RandRandom the fact that they tried defining a delegate which took a `ref` and that didn't work suggests that might not be the issue – canton7 Apr 08 '22 at 12:44
  • @MakePeaceGreatAgain that item is stored elsewhere, but that does not really matter now, as during debug session, values are still `null` the moment it steps out of `newItemPropertySetter`. – Soul Reaver Apr 08 '22 at 12:54
  • Related article - https://visualstudiomagazine.com/articles/2019/07/01/updating-linq.aspx – John Alexiou Apr 08 '22 at 12:55
  • Related [SO] post - https://stackoverflow.com/a/45868903/380384 – John Alexiou Apr 08 '22 at 12:57
  • 1
    @SoulReaver We need a repro that we can run, to see what you're seeing. Without that, we can't help – canton7 Apr 08 '22 at 14:15

1 Answers1

1

It's a guess because you didnt show enough code, but I would guess you are not taking "y" properly into closure in class B

MikolajR
  • 81
  • 4