-1

I'm trying to write a couple extensions for some types I'm working with. The base type is 'InputField'. 'ListField' inherits from 'InputField'. I'll show what I'm trying to do:

public static void LoadInputField(this InputField input, CustomField field)
{
    SetValues(ref input, field);
}

public static void LoadInputField(this ListField input, CustomField field)
{
    SetValues(ref input, field);

    var optionItems = (from o in field.CustomFieldOptions
                         select new ListItem(o.OptionLabel, o.CustomFieldOptionId.ToString()));
    input.AddChoices(optionItems.ToList());
}

private static void SetValues(ref InputField input, CustomField field)
{
    input.CustomFieldId = field.CustomFieldId;
    input.ResponseTitle = field.ColumnName;
    input.Prompt = field.ColumnCaption;
    input.DisplayOrder = field.SortOrder;
    input.Required = !string.IsNullOrEmpty(field.ColumnRequiredMessage);

    input.ErrorClass = "text-danger";
    if (input.Required)
        input.RequiredMessage = field.ColumnRequiredMessage;
}

The extension for the ListField type errors at SetValues(ref input, field);. The message says, 'The 'ref' argument type doesn't match parameter type.'

Perhaps this isn't the best way to do this, but I'm open to options.

M Kenyon II
  • 4,136
  • 4
  • 46
  • 94

2 Answers2

0

You can cast it on a local variable before you call the method:

InputField inputField = (InputField)input;
SetValues(ref inputField, field);

Apart from that i don't understand why you need ref, it works without casting if it's not ref. C# requires that any ref parameters be of the exact type.

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 2
    This will only work if the SetValues doesn't actually need to use the `ref` keyword. [see my discussion with Patrick Hofman](http://stackoverflow.com/a/30027307/3094533) in the comments to a similar answer I gave to someone a few monthes ago. – Zohar Peled Aug 12 '15 at 15:24
0

As per the suggestions in the comments, I dropped ref and the code works.

M Kenyon II
  • 4,136
  • 4
  • 46
  • 94