0

How can I change the signature of my Action delegates to NOT use void methods in my Dictionary<bool, Action<UrlParts, List<UrlParts>>>?

public static Func<UrlParts, List<UrlParts>, bool> LacksDomainPage = (up, lup) => !lup.Contains(up);

public static Action<UrlParts, List<UrlParts>> LacksUrl = (up, lup) => dicAddDomainPage[LacksDomainPage(up, lup)](up, lup);

public static Dictionary<bool, Action<UrlParts, List<UrlParts>>> dicAddDomainPage
                = new Dictionary<bool, Action<UrlParts, List<UrlParts>>>() { { true, AddDomainPage }, { false, AddNothing }, };

using

public static Action<UrlParts, List<UrlParts>> AddDomainPage = (up, ldp) => ldp.Add(up);
public static Action<UrlParts, List<UrlParts>> AddNothing = (up, ldp) => { };

instead of

public static void AddDomainPage(UrlParts up, List<UrlParts> ldp)
{
    ldp.Add(up);
}

public static void AddNothing(UrlParts up, List<UrlParts> ldp) { }

Edited by srider1969: spacing

2
Contributors
11
Replies
58
Views
1 Year
Discussion Span
Last Post by srider1969
0

Could you not use Func delegates instead of Action delegates?

Edited by ddanbe: typo

0

Thanks for your reply.

Since the program isn't returning anything, I am unsure how I would use Func. Could you elaborate?

0

Here is another example of what I am asking.

        public static Dictionary<string, Action> Testing = new Dictionary<string, Action>() 
        { 
            { "one", TestOne }, 
            { "two", TestTwo }, 
        };

        static void Main(string[] args)
        {
            Testing["one"]();
        }

        public static Action TestOne = () => { Debug.WriteLine("TestOne"); };
        public static Action TestTwo = () => { Debug.WriteLine("TestTwo"); };

Edited by srider1969: Incomplete

0

Try this:

class Program
    {   
        static void Main(string[] args)
        {
            Dictionary<string, Action> Testing = new Dictionary<string, Action>();
            Testing["one"] = new Action(TestOne); 
            Testing["two"] = new Action(TestTwo); 
            Testing["one"].Invoke();
            Testing["two"].Invoke();
            Console.ReadKey();
        }

        public static Action TestOne = () => { Console.WriteLine("TestOne"); };

        public static Action TestTwo = () => { Console.WriteLine("TestTwo"); };       
    }
0

I've been looking at the problem and stumbled my way to:

private static Dictionary<string, Action> dsa1 { get { return new Dictionary<string, Action>() { { "one", dsa1TestOne } }; } }
private static Dictionary<string, Action> dsa2 { get { return new Dictionary<string, Action>() { { "one", dsa2TestOne } }; } }

private static Dictionary<string, Action> dsa3 = new Dictionary<string, Action>()  { { "one", dsa1TestOne } };
private static Dictionary<string, Action> dsa4 = new Dictionary<string, Action>() { { "one", dsa2TestOne } };        

static void Main(string[] args)
{
    Dictionary<string, Action> dsa3_again = new Dictionary<string, Action>()  { { "one", dsa1TestOne } };
    Dictionary<string, Action> dsa4_again = new Dictionary<string, Action>() { { "one", dsa2TestOne } };        

    dsa1["one"]();
    dsa2["one"]();
    dsa3["one"]();
    dsa4["one"]();
    dsa3_again["one"]();
    dsa4_again["one"]();
}

public static Action dsa1TestOne = () => { Debug.WriteLine("TestOne"); };

public static void dsa2TestOne()
{
    Debug.WriteLine("TestOne");
}

Edited by srider1969: Un-needed code

0

Why would a Dictionary of string to Action's collection initializer require a void method at class scope?

Testing the above showed that dsa3 will throw a NullReferenceException on the Action delegate and not the void.

0

You not only have to instantiate the dictionary, but also the actions you put in it. See my example.

0

That is at the heart of the question. The collection initializer (for Generics or just the Dictionary?) only accepts void at runtime when Action is specified.

0

For the constructor at the class scope. My fourth post help me discover that the properties worked fine as well as the class level Action to void constructor and the program scope constructors.

I am just curious if that is by design or something else.

Edited by srider1969: incomplete

0

I am failing to see how I have NOT clearly demonstrated I know what an Action delegate is. Or how ANY of my POSTED code requires the need for a Func delegate.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.