开发者

Can a List hold multiple void methods?

im trying to create a ConsoleApplication in C#. Right now I'm working on a binding system that would read the key you input and take Actions if it is binded.

So far I created a struct Binded holding a ConsoleKey and a void Action() and I made a List Binds to put 开发者_JAVA百科it in a neat list.

public struct Binded  
        {   
            public ConsoleKey Key;  
            public void Action()  
            {  
//Whatever  
            }  
        }  
List<Binded> Binds

Then I just add the Keys I want to use along with the Action I want them to take. Now I can add the keys just fine but it seems like i'm unable to set a different Action() to each key. If you know what's the problem or you have a better idea on how to do it I'm eager to hear it, thanks in advance.


First off, I'd recommend using a class instead of a struct (or making this immutable).

That being said, you can do this by defining this to take a delegate to the action, not defining the Action in the struct/class itself.

For example:

public class Binding
{
     public Binding(ConsoleKey key, Action action)
     {
            this.Key = key;
            this.Action = action;
     }
     public ConsoleKey Key { get; private set; }
     public Action Action { get; private set; }
}

Then you would do:

public List<Binding> Binds;

// Later...
Binds.Add( new Binding(ConsoleKey.L, () => 
   {
       // Do something when L is pressed
   });
Binds.Add( new Binding(ConsoleKey.Q, () => 
   {
       // Do something when Q is pressed
   });


You should make a property of type Action (which is a delegate type)


Something like this should do the trick.

using System;
using System.Collections.Generic;

namespace ActionableList
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Actionable> actionables = new List<Actionable>
            {
                new Actionable
                    {
                        Key = ConsoleKey.UpArrow,
                        Action = ConsoleKeyActions.UpArrow
                    },
                new Actionable
                {
                    Key = ConsoleKey.DownArrow,
                    Action = ConsoleKeyActions.DownArrow
                },
                new Actionable
                {
                    Key = ConsoleKey.RightArrow,
                    Action = ConsoleKeyActions.RightArrow
                },
                new Actionable
                {
                    Key = ConsoleKey.UpArrow,
                    Action = ConsoleKeyActions.UpArrow
                }
            };

            actionables.ForEach(a => a.Action());

            Console.ReadLine();
        }
    }

    public class Actionable
    {
        public ConsoleKey Key { get; set; }
        public Action Action { get; set; }
    }

    public static class ConsoleKeyActions
    {
        public static void UpArrow()
        {
            Console.WriteLine("Up Arrow.");
        }

        public static void DownArrow()
        {
            Console.WriteLine("Down Arrow.");
        }

        public static void LeftArrow()
        {
            Console.WriteLine("Left Arrow.");
        }

        public static void RightArrow()
        {
            Console.WriteLine("Right Arrow.");
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜