Definition of non-generic method?

3

In

public partial class frmAltInfo : Form

The error occurs:

  

Extension method must be defined in non-generic static class

I do not understand what can be and how to solve.

Here was the error:

public static string TextNoFormatting(this MaskedTextBox _mask)

{
  _mask.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
    String retString = _mask.Text;
    _mask.TextMaskFormat = MaskFormat.IncludePromptAndLiterals;
    return retString;
}
    
asked by anonymous 23.11.2014 / 23:32

1 answer

5

The error is not in this section; is elsewhere. You must have defined an extension method within this class and you can not define it so it is not static. As this excerpt makes it clear that the class can not be static the problem is in the defined method. Find him. It has a parameter with this .

Since the class is partial it must have another part of it in another file. The problem-giving snippet may be in this other file.

If you really need this method as an extension you should create a separate static class for it.

Another solution is to make this method normal rather than extension by removing the modifier from the first parameter this . It would look like this:

public static string TextNoFormatting(MaskedTextBox _mask) {
    _mask.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
    String retString = _mask.Text;
    _mask.TextMaskFormat = MaskFormat.IncludePromptAndLiterals;
    return retString;
}

I placed GitHub for future reference .

Simple removal of this before parameter _mask solves problem.

As sidenote this variable nomenclature is out of standard. There is no reason to use the underline.

Extension Methods Documentation .

Only extension methods have constraints on how the class should be defined. Normal static methods can be in any class. The question is only if it should be in the class, what will it do, access.

    
23.11.2014 / 23:44