locked
When is Action.Invoke vs class method used? RRS feed

  • Question

  • Hello,

     I have been coding some of my projects with Dictionary<enum,Action> objectConatiner and using the

    objectContainer[ enum ].Invoke(). Which just calls a method based on the enum object. See code snippet;

    // Enum setup

    [Flags] private enum DirectionControl { UP = 0x01, DOWN = 0x02, LEFT = 0x04, RIGHT = 0x08, RESET = 0xFF }

    // Container setup

    // this.MoveHandler = new Dictionary<DirectionControl, Action>() { { DirectionControl.RESET, this.MoveReset }, { DirectionControl.UP, this.MoveUp }, { DirectionControl.DOWN, this.MoveDown }, { DirectionControl.LEFT, this.MoveLeft }, { DirectionControl.RIGHT, this.MoveRight } };

    // Example delegate method

    // private void MoveReset() { this.Location = new Point( ( Screen.PrimaryScreen.WorkingArea.Width / 2 ) - ( this.Width / 2 ), ( Screen.PrimaryScreen.WorkingArea.Height / 2 ) - ( this.Height / 2 ) ); }

    // Example caller

    //

    this.ToggleXHandler[ this.TogglePosX ].Invoke();

     Before, I coded Switch statements to call methods based on object int values. In some projects the Switch/Case

    was getting messy. So, as for a solution, I tried Dictionary<enum,action> statement and code Delegates.  Is or was

    this correct for best practices?  Helpful comments and suggestions are welcome.

     

     Thanks :)

    Thursday, September 20, 2018 2:15 PM

All replies

  • It doesn't matter, whether you use switch or your dictionary approach. They are equivalent. Cause you now need the conditional to evaluate your enum.

    Assuming ToggleXHandler points to MoveHandler and TogglePosX is of your enum type, then you don't need the Invoke().

    this.ToggleXHandler[this.TogglePosX]();

    is sufficient.

    For more suggestions, you need to explain your use-case and provide more information and context.

    Thursday, September 20, 2018 8:15 PM
  • Hello,

     I have been coding some of my projects with Dictionary<enum,Action> objectConatiner and using the

    objectContainer[ enum ].Invoke(). Which just calls a method based on the enum object. See code snippet;

    // Enum setup

    [Flags] private enum DirectionControl { UP = 0x01, DOWN = 0x02, LEFT = 0x04, RIGHT = 0x08, RESET = 0xFF }

    // Container setup

    // this.MoveHandler = new Dictionary<DirectionControl, Action>() { { DirectionControl.RESET, this.MoveReset }, { DirectionControl.UP, this.MoveUp }, { DirectionControl.DOWN, this.MoveDown }, { DirectionControl.LEFT, this.MoveLeft }, { DirectionControl.RIGHT, this.MoveRight } };

    // Example delegate method

    // private void MoveReset() { this.Location = new Point( ( Screen.PrimaryScreen.WorkingArea.Width / 2 ) - ( this.Width / 2 ), ( Screen.PrimaryScreen.WorkingArea.Height / 2 ) - ( this.Height / 2 ) ); }

    // Example caller

    //

    this.ToggleXHandler[ this.TogglePosX ].Invoke();

     Before, I coded Switch statements to call methods based on object int values. In some projects the Switch/Case

    was getting messy. So, as for a solution, I tried Dictionary<enum,action> statement and code Delegates.  Is or was

    this correct for best practices?  Helpful comments and suggestions are welcome.

     

     Thanks :)

    1) First of all, I'd include in the enum, the name None with value 0.

    2) Second, I'd never use value 0xFF for Reset, because it is not a combination of all other values (this makes me think you don't need the attribute Flags).

    3) I'd use, definitely, a single dimension array of Actions, combined with an enum without Flags attribute:

                var p=new SomeProperType();
                p.MHandler=new Action[Enum.GetNames(typeof(DirectionControl)).Count()];
                p.MHandler[(int)DirectionControl.None]=()=>p.None();
                p.MHandler[(int)DirectionControl.Up]=()=>p.Up();
                p.MHandler[(int)DirectionControl.Down]=()=>p.Down();
                p.MHandler[(int)DirectionControl.Left]=()=>p.Left();
                p.MHandler[(int)DirectionControl.Right]=()=>p.Right();
                p.MHandler[(int)DirectionControl.Reset]=()=>p.Reset();
    

    Thursday, September 20, 2018 9:10 PM
  • Good grief!

    Ritehere, that was a reasonable, helpful post that didn't call anybody names or use the word 'rubbish'. Are you feeling okay? I hope you are not ill.

    Thursday, September 20, 2018 11:43 PM
  • :) exactly my thoughts.
    Friday, September 21, 2018 11:23 AM
  • :) exactly my thoughts.

    mr. ultraLowQualityPoints:
    Why do you need to hide yourself behind someone else's reply? Should we all consider you a coward? Al"rite", so, besides being mr. ultraLowQualityPoints, you possess ultraLowQuality moral values... :)

    I'm not surprised that your friend @bonnie has not accused you of bullying... :S

    Monday, September 24, 2018 4:50 AM
  •  I hope you are not ill.


    You got it! I had a little headache, seeing too much and abusive rubbish# codes...

     Are you feeling okay?


    Definitely yes! It was promptly cured. Anyway, thanks for worrying about it...

    Ritehere, that was a reasonable, helpful post ...


    Yes, pretty much helpful, cause it is highly elevated and advanced C# code. Notice, that, any rubbish# coder would use a Dictionary<> as usual, and I'm just showing them that there's life beyond Dictionary<>, List<>, int and string. :)

    ...post that didn't call anybody names or use the word 'rubbish'.


    I didn't call anybody 'rubbish'. Understand that C# is a language; rubbish# is also a language; people who implement code in C# Language are C# coders; people that implement code in rubbish# language, are rubbish# coders, i.e., the code is rubbish# because of the language (and not the person).

    Good grief!


    For all rubbish# coders (not me :).

    Monday, September 24, 2018 5:04 AM
  •  I hope you are not ill.


    You got it! I had a little headache, seeing too much and abusive rubbish# codes...

     Are you feeling okay?


    Definitely yes! It was promptly cured. Anyway, thanks for worrying about it...

    Ritehere, that was a reasonable, helpful post ...


    Yes, pretty much helpful, cause it is highly elevated and advanced C# code. Notice, that, any rubbish# coder would use a Dictionary<> as usual, and I'm just showing them that there's life beyond Dictionary<>, List<>, int and string. :)

    ...post that didn't call anybody names or use the word 'rubbish'.


    I didn't call anybody 'rubbish'. Understand that C# is a language; rubbish# is also a language; people who implement code in C# Language are C# coders; people that implement code in rubbish# language, are rubbish# coders, i.e., the code is rubbish# because of the language (and not the person).

    Good grief!


    For all rubbish# coders (not me :).

    Monday, September 24, 2018 5:04 AM
  • :) exactly my thoughts.

    By the way, do you have good thoughts?

    Like suggesting to change the type of a C# object (using reflection)?

    Maybe like reading a bunch of bytes from a file, interpreting them as you wish, and saying it is a TEXT file? Have you ever heard of standards?

    How many years of (bad) programming have you?

    How many more do you estimate you'll take to understand basic programming concepts?

    Have already learned how to split a CSV line? What about property's backing fields?

    Monday, September 24, 2018 5:10 AM
  • Greetings ritehere.

    It's good to know you are okay. This forum would not be the same without you.

    Monday, September 24, 2018 5:29 AM
  • Greetings ritehere.

    It's good to know you are okay. This forum would not be the same without you.

    Have you noticed it?

    People in this forum have learned (or are learning) how to win the red squiggy lines, but forgot to learn the the Language ( C# ) and basic programmings concept.

    That's why I'm doing this great effort to transform this forum in a real technical forum about C# Language!

        ... but even the mvps(?!) are really weak in C# language...

    By the way, what's your code for timeout issue in the WebClient.DownloadStringTaskAsync?

    Monday, September 24, 2018 5:44 AM