Extension methods must be defined in a non-generic static class
  • hi all i am trying below function in my windows form but i am facing this 
    error Extension methods must be defined in a non-generic static class

     public static string Right(this string value, int length)
            {
                if (String.IsNullOrEmpty(value)) return string.Empty;

                return value.Length <= length ? value : value.Substring(value.Length - length);
            }
  • 2 Comments sorted by
  • Vote Up0Vote Down farazfaraz
    10.00 Karma Accepted Answer
    remove this word with your parameter.

    replace your code with below code.


    public static string Right(string value, int length)
    {
    if (String.IsNullOrEmpty(value)) return string.Empty;

    return value.Length <= length ? value : value.Substring(value.Length - length);
    }
  • You need to create a static class and add static methods in it.
    public static class StringExtensions
    {
    public static string Right(string value, int length)
    {
    if (String.IsNullOrEmpty(value)) return string.Empty;

    return value.Length <= length ? value : value.Substring(value.Length - length);

    } - See more at:
    http://www.codingresolved.com/discussion/2591/extension-methods-must-be-defined-in-a-non-generic-static-class#sthash.J9A3AFs1.dpuf//
    //add static methods here
     
    public static string Right(string value, int length)
    {
    if (String.IsNullOrEmpty(value)) return string.Empty;

    return value.Length <= length ? value : value.Substring(value.Length - length);

    } - See more at:
    http://www.codingresolved.com/discussion/2591/extension-methods-must-be-defined-in-a-non-generic-static-class#sthash.J9A3AFs1.dpuf
    }

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Tagged

Popular Posts of the Week

Top Posters Weekly

Optimum Creative