Definition of non-generic method?

Asked

Viewed 1,312 times

3

In

public partial class frmAltInfo : Form

Error occurs:

Extension method must be defined in a non-generic Static class

I don’t understand what it can be or how to fix it.

Here was the mistake:

public static string TextNoFormatting(this MaskedTextBox _mask)

{
  _mask.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
    String retString = _mask.Text;
    _mask.TextMaskFormat = MaskFormat.IncludePromptAndLiterals;
    return retString;
}
  • 1

    Place the code and error separately instead of the screen. This way it is difficult to find your question in searchers.

  • Besides this. Close, nowhere else has a parameter with this. ?

  • The ideal is to put only what is affecting you but if you don’t know where it is, it would be better. Note that the class is partial so there is another part of it somewhere.

  • It turns out that all the other classes are exactly the same as this, however, this error does not occur. I’m killing myself all day to be able to tidy up.

1 answer

6


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

How class is partial must have another part of it in another file. The part that gives problem may be in this other file.

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

Another solution is to make this method normal and not extension by removing the modifier from the first parameter this. 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 put in the Github for future reference.

The simple withdrawal of thisbefore the parameter _mask solves the problem.

Like Sidenote this variable nomenclature is non-standard. There is no reason to use underscore.

Documentation on extension methods.

Only extension methods have constraints on how the class should be defined. Normal static methods can be in any class. The only question is whether he should be in the class, what he’ll do, access.

  • Ah man, I found the mistake, thank you very much!

  • Another question, how should the class accept a Static method?

  • If that’s all static There’s not much restriction on what class should look like. But think carefully if the class really needs a static member, most of the time if the class is not utilitarian (doing isolated tasks) static methods are little needed. And in case this method would be utility too.

  • In this case need, because it is to do something specific. I really appreciate your help.

  • That’s right! What a garbage error message

  • @Son, I think she’s good

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.