C# Constructor that takes in two Action<objects> Fails -
there might easier way i'm doing i'm curious why can't call constructor 2 functions.
i'm trying write sort of sequencer wrapper doe step1, kick off step2, kick off step3, etc.
the program fails when call constructor stepmonitor
error i'm getting: cannot implicitly convert type 'void' 'system.action
static void main(string[] args) { step1 step1 = new step1(); step2 step2 = new step2(); stepmonitor stepmonitor = new stepmonitor(step1.step1go(), step2.step2go()); // fails here } public class stepmonitor { #region function monitor action<object> _objecttomonitor; #endregion #region function execute on event action<object> _objecttoexecute; #endregion #region constructor public stepmonitor(action<object> objecttomonitor, action<object> objecttoexecute) { _objecttomonitor = objecttomonitor; _objecttoexecute = _objecttoexecute; _objecttomonitor += _objecttoexecute; } #endregion } public class step1 { public event eventhandler stepcompletedhandler; // event public step1() { } public void step1go() { console.writeline("enter string step1"); string step1 = console.readline(); triggerstepcompleted(); } protected virtual void triggerstepcompleted() // trigger. foo calls raise event { // make copy more thread-safe eventhandler handler = stepcompletedhandler; if (handler != null) { // invoke subscribed event-handler(s) handler(this, null); } } } public class step2 { public event eventhandler stepcompletedhandler; // event public step2() { } public void step2go() { console.writeline("enter string step2"); string step2 = console.readline(); triggerstepcompleted(); } protected virtual void triggerstepcompleted() // trigger. foo calls raise event { // make copy more thread-safe eventhandler handler = stepcompletedhandler; if (handler != null) { // invoke subscribed event-handler(s) handler(this, null); } } }
you're calling step1go , step2go - whereas want using method group conversion create action delegates:
stepmonitor stepmonitor = new stepmonitor(step1.step1go, step2.step2go); additionally, methods don't take parameters, either need change stepmonitor accept action instead of action<object> or use anonymous function explicitly ignore parameter:
stepmonitor stepmonitor = new stepmonitor(_ => step1.step1go(), _ => step2.step2go()); here _ somewhat-conventional name parameter ignored. use:
stepmonitor stepmonitor = new stepmonitor(ignored => step1.step1go(), ignored => step2.step2go()); note none of related constructors specifically. you'd same problem @ moment if wrote:
action<object> action = step1.step1go(); whereas i'm suggesting should using 1 of these forms:
action action = step1.step1go; action<object> action = ignored => step1.step1go();
Comments
Post a Comment