0

I have a class that exposes a IObservable. This is how:

  private readonly Subject<(int moduleNumber, int channelNumber, object oldValue, object newValue)> _channelChanged =
        new Subject<(int moduleNumber, int channelNumber, object oldValue, object newValue)>();

    /// <summary>
    /// Subscribe to this if you want to be notified as soon as a channel changed its value.
    /// </summary>
    public IObservable<(int moduleNumber, int channelNumber, object oldValue, object newValue)> ChannelChanged => _channelChanged.AsObservable();

I created an instance of this class an tried to subscribe to this IObservable like this:

 public void TestDataModelEventWhenAnalogueChannelChanged()
    {
        var instance = new MyClassThatContainsTheIObservable();

        IDisposable subscription = instance.ChannelChanged.Subscribe(OnChannelChanged);

        // do stuff that leads to a channel changing its value
    }

    private void OnChannelChanged((int moduleNumber, int channelNumber, object oldValue, object newValue) e)
    {
        // Assert the correct arguments appear here
    }

The compiler says that he can't convert from a method group to an IObserver. I get that - intellisense tells me that I need to provide an IObserver to Subscribe(). But then I found this tutorial:

https://rehansaeed.com/reactive-extensions-part1-replacing-events/

If you look at his example of subscribing you see that he just provides a method as well. Looking at the answer of the following question it seems to be possible to subscribe with a lambda-expression, as well:

Reactive Observable Subscription Disposal

When trying this, my compiler tells me the same: He can't convert a lambda expression to IObserver.

What am I doing wrong? Do I really have to create a whole class that implements IObserver?

selmaohneh
  • 553
  • 2
  • 17

1 Answers1

-1

I'm guessing you've missed using System;. The Subscribe extension which accepts an action is defined in the System namespace, because IObservable itself is defined in that namespace.

Tolik Pylypchuk
  • 680
  • 1
  • 6
  • 15