1

I am using one delegate and event for the same as below:

public delegate void DelgSampledelegate(string status);

public static event DelgSampledelegate sampleEvent;

sampleEvent += new DelgSampledelegate(sample_method);

public void sample_method(string value)
{}

Now I wanted to use Rx Extension for above delegate. So I tried below code to create object of Observable.FromEvent

var objDataUpdated = Observable.FromEvent(h => sampleEvent += h, h => sampleEvent -= h);

var movesSubscription = objDataUpdated.Subscribe(evt => evt.ToString());

My aim is to call 'sample_method' function as I was calling earlier.I know it may be done either via subscribe.Please guide me proper way to do it.

supertopi
  • 3,469
  • 26
  • 38
Sushil
  • 442
  • 3
  • 10
  • 23
  • possible duplicate of [How to use Observable.FromEvent instead of FromEventPattern and avoid string literal event names](http://stackoverflow.com/questions/19895373/how-to-use-observable-fromevent-instead-of-fromeventpattern-and-avoid-string-lit) – James World Feb 27 '15 at 08:24

1 Answers1

1

Can't see that you're missing much except the type parameters and actually raising an event to test it:

var objDataUpdated = Observable.FromEvent<DelgSampledelegate, string>(
    h => sampleEvent += h,
    h => sampleEvent -= h);

var movesSubscription = objDataUpdated.Subscribe(x => sample_method(x));

// note the preceding line can be shortened to finish
// .Subscribe(sample_method) but I didn't want to make too many leaps

// raise a sampleEvent, will call sample_method("Test")
sampleEvent("Test");

See How to use Observable.FromEvent instead of FromEventPattern and avoid string literal event names for a comprehensive explanation of FromEvent - although note the form in this answer needs no conversion function because DelgSampledelegate's signature matches the required OnNext signature.

Community
  • 1
  • 1
James World
  • 29,019
  • 9
  • 86
  • 120