1

Is there any way to use WhenAny for the property of a value that is currently null and will be set later?
Like this:

public Server SelectedServer {get;set;}

public TestClass()
{
    this.WhenAny(SelectedServer.Items.Changed, x => x).Subscribe(/*Do something*/);
}  

SelectedServer is initially null, will be set by through user interaction and therefor I always get a NullReferenceException in the Constructor.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
Tobi
  • 183
  • 1
  • 10
  • just do the subscribe when you set the `SelectedServer` - it's a good idea to unsubscribe any old subscription then too - as it is right now this will never work as C# is evaluating the arguments before passing them on (so you would need either a function or a `Lazy` value to pass into `WhenAny` and then `WhenAny` could not know when it is safe to use those ;)) – Random Dev Mar 28 '15 at 14:30
  • Would this also work if I want to use the WhenAny for an ObservableAsPropertyHelper (i.e. with ToProperty())? – Tobi Mar 28 '15 at 15:24

1 Answers1

2

This is a typo that instead produces a runtime crash, it should be:

this.WhenAnyValue(x => x.SelectedServer.Items.Changed);

But given the name of your variable, I'm guessing a better thing would be:

this.WhenAnyObservable(x => x.SelectedServer.Items.Changed);
Ana Betts
  • 73,868
  • 16
  • 141
  • 209
  • That was it :) And after reading about WhenAnyObservable, you are probably right with that one too. However I'm still not very sure about the distinction between WhenAny and WhenAnyObservable. Do I use it generally when I'm referencing another object's property, in case the object itself gets replaced? I.e. if I simply replace x.SelectedServer.Items.Changed with x.SelectedServer.ItemCount in the sample above. – Tobi Mar 30 '15 at 21:21
  • You should use WhenAnyObservable whenever the last item in your chain is of type IObservable – Ana Betts Mar 31 '15 at 06:17
  • Any idea why the WhenAnyObservable doesn't fire when I set SelectedServer from null to an actual instance that already has items (Items is of type IReactiveDerivedList and has ChangeTrackingEnabled set to true)? – Tobi Mar 31 '15 at 11:51
  • @TobiasZimmermann It won't fire unless there is a change - it doesn't fire as soon as you assign the instance – Ana Betts Mar 31 '15 at 17:47
  • Ok, my solution would be to combine WhenAnyObservable(Items.Changed) with WhenAnyValue(SelectedServer), like you've shown [here](http://stackoverflow.com/questions/21317264/reactiveui-how-to-use-whenanyobservable-properly). However using it with a selector doesn't seem to work anymore. I'm now trying to use Observable.CombineLatest for that but I'm stuck on the problem that Items.Changed doesn't fire unless there's actual change to the List. WhenAnyObservable(Items.Changed.StartsWith(...)) gives me an exception and WhenAnyObservable(Items.Changed).StartsWith(...) doesn't trigger anything. – Tobi Mar 31 '15 at 19:59
  • 1
    I was able to make it work. StartsWith doesn't work with "null" and I tried to pass an empty list instead, which of course means StartsWith shouldn't start with anything -.- – Tobi Apr 01 '15 at 13:48