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;
};
}
}
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 aFunc<arg1,arg2,arg3,return_type>Here
Func<string, TextBoxSettings>designates a method that accepts a string parameter (input) and returns aTextBoxSettings(output).Also, since you need an input argument in
TextBoxSettingsMethodit cannot be a property. I changed it to be a method.Also I changed the test to make sure that
textBoxSettingsMethodis not null (not the result is returning when you execute it). The testtextBoxSettingsMethod(txtBoxName) == nullcalls the method and tests if the return value of typeTextBoxSettingsisnull. If you only want to know whether the method is defined, the test should betextBoxSettingsMethod == null.Do not confuse the expression
textBoxSettingsMethodwhich is the method (or delegate) andtextBoxSettingsMethod(txtBoxName)which executes the method.What is the argument
string txtBoxNamegood 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 txtBoxNameis not required, only the argument of the lambda expressiontbNamewhich will require an actual parameter value when called):In contrast, this is a method that creates different methods that return alwas the same settings
UPDATE #2
Maybe you want this
You can use it like this:
But I’m still not sure why you want to create these methods dynamically. A simple method returning the setting would suffice