简体   繁体   中英

Cannot assign to 'textBoxSettingsMethod' because it is a 'method group'

I have reviewed about 20 plus pages on the web regarding this error. I have been unable to find a solution to this. I know why the error is occurring but cannot figure out how to fix this. :

I just need to assign an action to my action, but these actions must have an incoming parameter of type string. Any ideas are greatly appreciated as I have been struggling with this for quite some time.

public class CommonDemoHelper
{
    static Action<TextBoxSettings> textBoxSettingsMethod(string txtBoxName);
    static Action<DateEditSettings> dateEditSettingsMethod;
    static Action<LabelSettings> labelSettingsMethod;
    static Action<LabelSettings> wideLabelSettingsMethod;
    static Action<SpinEditSettings> spinEditSettingsMethod;
    static Action<MemoSettings> memoSettingsMethod;

    public static Action<TextBoxSettings> TextBoxSettingsMethod(string txtBoxName)
    {
        get
        {

            if (textBoxSettingsMethod(txtBoxName) == null)
                //Getting error below cannot assign to method group
                textBoxSettingsMethod = CreateTextBoxSettingsMethod(txtBoxName);
            return textBoxSettingsMethod(txtBoxName);
        }
    }

    static Action<TextBoxSettings> CreateTextBoxSettingsMethod(string txtBoxName)
    {
        return settings =>
        {
            settings.ControlStyle.CssClass = "editor";
            settings.ShowModelErrors = true;
            settings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText;
        };
    }
}

You're declaring textBoxSettingsMethod as a method:

static Action<TextBoxSettings> textBoxSettingsMethod(string txtBoxName);

when it looks like you're trying to define it as a property:

static Action<TextBoxSettings> textBoxSettingsMethod;

Although since txtBoxName is not used in CreateTextBoxSettingsMethod it's a little confusing as to what you're actually trying to accomplish.

If a method (or delegate) returns a value, it will be a Func<return_type> , not an action. If it has input parameters (arguments) then it would be a Func<arg1,arg2,arg3,return_type>

static Func<string, TextBoxSettings> textBoxSettingsMethod;

public static Func<string, TextBoxSettings> GetTextBoxSettingsMethod(string txtBoxName)
{
    if (textBoxSettingsMethod == null)
        textBoxSettingsMethod = CreateTextBoxSettingsMethod(txtBoxName);
    return textBoxSettingsMethod;
}

static Func<string, TextBoxSettings> CreateTextBoxSettingsMethod(string txtBoxName)
{
    ...
}

Here Func<string, TextBoxSettings> designates a method that accepts a string parameter (input) and returns a TextBoxSettings (output).

Also, since you need an input argument in TextBoxSettingsMethod it cannot be a property. I changed it to be a method.

Also I changed the test to make sure that textBoxSettingsMethod is not null (not the result is returning when you execute it). The test textBoxSettingsMethod(txtBoxName) == null calls the method and tests if the return value of type TextBoxSettings is null . If you only want to know whether the method is defined, the test should be textBoxSettingsMethod == null .

Do not confuse the expression textBoxSettingsMethod which is the method (or delegate) and textBoxSettingsMethod(txtBoxName) which executes the method.


What is the argument string txtBoxName good for? You are not using it. Do you need different methods for different textboxes? It's difficult to say.


UPDATE

This creates always the same method that returns different settings depending on the textbox name (the argument string txtBoxName is not required, only the argument of the lambda expression tbName which will require an actual parameter value when called):

static Func<string, TextBoxSettings> CreateTextBoxSettingsMethod()
{
    return tbName =>
    {
        TextBoxSettings settings;
        switch (tbName) {
            case "textbox1":
            case "textbox2":
            case "textbox3":
                settings = new TextBoxSettings {
                    TextBoxName = tbName,
                    ControlStyle = "editor",
                    ShowModelErrors = true
                };
                settings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText;
                return settings;
            case "textbox4":
            case "textbox5":
                settings = new TextBoxSettings {
                    TextBoxName = tbName,
                    ControlStyle = "displayer",
                    ShowModelErrors = false
                };
                settings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.TextOnly;
                return settings;
            default:
                return new TextBoxSettings { TextBoxName = tbName };
        }
    };
}

In contrast, this is a method that creates different methods that return alwas the same settings

static Func<string, TextBoxSettings> CreateTextBoxSettingsMethod(string txtBoxName)
{
    switch (txtBoxName) {
        case "textbox1":
        case "textbox2":
        case "textbox3":
            return tbName =>
            {
                var settings = new TextBoxSettings {
                    TextBoxName = tbName,
                    ControlStyle = "editor",
                    ShowModelErrors = true
                };
                settings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.ImageWithText;
                return settings;
            };
        case "textbox4":
        case "textbox5":
            return tbName =>
            {
                var settings = new TextBoxSettings {
                    TextBoxName = tbName,
                    ControlStyle = "displayer",
                    ShowModelErrors = false
                };
                settings.Properties.ValidationSettings.ErrorDisplayMode = ErrorDisplayMode.TextOnly;
                return settings;
            };
        default:
            return tbName => new TextBoxSettings { TextBoxName = tbName };
    }
}

UPDATE #2

Maybe you want this

static Dictionary<string, Func<string, TextBoxSettings>> textBoxSettingsMethod
    = new Dictionary<string,Func<string,TextBoxSettings>>();

public static Func<string, TextBoxSettings> GetTextBoxSettingsMethod(string txtBoxName)
{
    Func<string, TextBoxSettings> method;
    if (!textBoxSettingsMethod.TryGetValue(txtBoxName, out method)) {
        method = CreateTextBoxSettingsMethod(txtBoxName);
        textBoxSettingsMethod.Add(txtBoxName, method);
    }
    return method;
}

You can use it like this:

Func<string, TextBoxSettings> method;
TextBoxSettings setting;

method = GetTextBoxSettingsMethod("textbox1");
setting = method("textbox1");

// or

setting = GetTextBoxSettingsMethod("textbox1")("textbox1");

// or, if you are sure that all the methods have been assigned

method = textBoxSettingsMethod["textbox1"];
setting = method("textbox1");

// or

setting = textBoxSettingsMethod["textbox1"]("textbox1");

But I'm still not sure why you want to create these methods dynamically. A simple method returning the setting would suffice

public static TextBoxSettings GetTextBoxSettings(string txtBoxName)
{
    switch (txtBoxName) {
        // ...
        default:
            return null;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM