Answered Help Convert to vb.net .net 3.5(VS 2008)

  • miércoles, 25 de abril de 2012 15:33
     
      Tiene código

    Hello a create a extension method in C# .net 3.5 , with the help of this forum to catch the selection change in cells [msexcel 2007], and i like to now if it is possible convert this to vb.net vs2008?

    This is the static method.

     static class ExcelExtensions
        {
            public static IObservable<Excel.Range> WhenCellSelect(this Excel.Workbook WB)
            {
                var t=  Observable.Create<Excel.Range>(observer =>
                {
                    Excel.WorkbookEvents_SheetSelectionChangeEventHandler handler =
                        (Object sh,Excel.Range target) =>
                        {
                            observer.OnNext(target);
    
                        };
    
                    WB.SheetSelectionChange += handler;
                    
    
                    return () => WB.SheetSelectionChange -= handler;
                });
    
                return t;
            }
    Thank's,
      sorry for my english



    • Editado carman68 miércoles, 25 de abril de 2012 15:40
    •  

Todas las respuestas

  • miércoles, 25 de abril de 2012 16:49
     
     

    Hi,

    Yes, it's possible to write your code in VB.NET instead of C#.  Try asking your question in the VB.NET forum for help converting from C# to VB.NET.

    Though your query can be simplified by using Observable.FromEvent or Observable.FromEventPattern.

    - Dave


    http://davesexton.com/blog

  • miércoles, 25 de abril de 2012 17:29
     
     

    Thank's Dave i try the other forum for the conversion.

    in C# how to simplify? can you show how or point directions (i newbie in rx thank's).

    carman

  • miércoles, 25 de abril de 2012 18:23
     
      Tiene código

    Hi Carman,

    It should look something like this, though I haven't tested it myself.

    public static IObservable<Excel.Range> WhenCellSelect(this Excel.Workbook WB)
    {
    	return Observable.FromEvent<Excel.WorkbookEvents_SheetSelectionChangeEventHandler, Excel.Range>(
    		h => WB.SheetSelectionChange += h, 
    		h => WB.SheetSelectionChange -= h);
    }

    - Dave


    http://davesexton.com/blog

  • domingo, 29 de abril de 2012 8:20
     
      Tiene código

    Hi, Dave Thank's to point the direction, but i receved a 

    ArgumentException "Error binding to target method." with this StackTrace:

    at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method, Boolean throwOnBindFailure) at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method) at System.Reactive.Linq.Observable.<>c__DisplayClass19c`2.<fromevent>b__19a(IObserver`1 observer) at System.Reactive.AnonymousObservable`1.<>c__DisplayClass1.<subscribe>b__0() at System.Reactive.Concurrency.Scheduler.Invoke(IScheduler scheduler, Action action) at System.Reactive.Concurrency.ScheduledItem`2.InvokeCore() at System.Reactive.Concurrency.ScheduledItem`1.Invoke() at System.Reactive.Concurrency.CurrentThreadScheduler.Trampoline.Run() at System.Reactive.Concurrency.CurrentThreadScheduler.Schedule[TState](TState state, TimeSpan dueTime, Func`3 action) at System.Reactive.Concurrency.CurrentThreadScheduler.Schedule[TState](TState state, Func`3 action) at System.Reactive.Concurrency.Scheduler.Schedule(IScheduler scheduler, Action action) at System.Reactive.AnonymousObservable`1.Subscribe(IObserver`1 observer) at System.ObservableExtensions.Subscribe[TSource](IObservable`1 source, Action`1 onNext, Action`1 onError, Action onCompleted) at System.ObservableExtensions.Subscribe[TSource](IObservable`1 source, Action`1 onNext) at ReactiveProgrammingConsole.InteropLab.Main() in D:\Metropolitano\Escalas\Excel_RX_events_C\Excel_RX_events_C\Program.cs:line 88</subscribe></fromevent>

    I suspect that is in signature of method , the second parameter await a TEventArg and I am giving an Excel.Range, it is??

    how to wrap the arguments of the excel workbook in TEventsAgs?

    I call the method this way

     try
                    {
                        WB.WhenCellSelect().Subscribe(cell => Console.WriteLine(cell.Row));
                    }
                    catch(ArgumentException ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                    }

    Thank's

    carman68


  • domingo, 29 de abril de 2012 10:18
     
     Respondida Tiene código

    Hi,

    Try specifying the delegate conversion parameter:

    public static IObservable<Excel.Range> WhenCellSelect(this Excel.Workbook WB)
    {
    	return Observable.FromEvent<Excel.WorkbookEvents_SheetSelectionChangeEventHandler, Excel.Range>(
    		h => h,  // delegate conversion
    		h => WB.SheetSelectionChange += h, 
    		h => WB.SheetSelectionChange -= h);
    }

    - Dave


    http://davesexton.com/blog

    • Editado Dave Sexton domingo, 29 de abril de 2012 10:19 Forgot a comma
    • Marcado como respuesta carman68 lunes, 30 de abril de 2012 1:06
    •  
  • lunes, 30 de abril de 2012 1:07
     
     

    Thank's Dave

    apreciate the support

    carman