简体   繁体   中英

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

I Have A Problem. After I Rename This Web Form I Get This Error But I Change Everything To New Name But I Get This Error. Plz, Help. Code :

using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class bVoteAnswer : System.Web.UI.Page
{
    UserVotes ue = new UserVotes();
    bVotes bv = new bVotes();
    bVoteAnswers ve = new bVoteAnswers();
    public void Page_Load(object sender, EventArgs e)
    {

        dropdownlist.Enabled = false;
        int BuildingId = Convert.ToInt32(Session["BuildingId"]);
        DataTable dt = new DataTable();
        dt = bv.Select(0, "", "", "", BuildingId, "",0);
        Grid_Vote.DataSource = dt;
        Grid_Vote.DataBind();

    }
protected void Grid_Vote_RowCreated(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[0].Visible = false;
}

protected void Grid_Vote_SelectedIndexChanged(object sender, EventArgs e)
{
    dropdownlist.Enabled = true;
    int VoteId = Convert.ToInt32(Grid_Vote.SelectedRow.Cells[0].Text);
    DataTable dt = new DataTable();
    dt = ve.Select(0,VoteId,"");
    for (int i = 0; i <= dt.Rows.Count; i++)
    {
        ListItem l = new ListItem(dt.Rows[i][2].ToString(), dt.Rows[i][2].ToString());
        dropdownlist.Items.Add(l);
    }
}

protected void btn_insert_Click(object sender, EventArgs e)
{
    int OwnerId = Convert.ToInt32(Session["OwnerId"]);
    int VoteId = Convert.ToInt32(Grid_Vote.SelectedRow.Cells[0].Text);
    DataTable dt = new DataTable();
    dt = ve.Select(0, VoteId, dropdownlist.SelectedValue);
    int AnswerId = Convert.ToInt32(dt.Rows[0][0]);
    DateTime ClientDateTime = DateTime.Now;
    string PersianDate = GetPersianDate(ClientDateTime);
    ue.Insert(0, VoteId, OwnerId, AnswerId, PersianDate);
}


public static string GetPersianDate(this DateTime date)
{
    PersianCalendar jc = new PersianCalendar();
    return string.Format("{0:0000}/{1:00}/{2:00}", jc.GetYear(date), jc.GetMonth(date), jc.GetDayOfMonth(date));
}

}

I Change The Name To First Name But The Error Display I Think It IS Not a error and Visual Studio Don't Run God

Extension methods need to be in a static class.

public partial class bVoteAnswer

This is not a static class. Move the function to a static class.

public static class ExtensionMethods
{
   public static string GetPersianDate(this DateTime date)
    {
        PersianCalendar jc = new PersianCalendar();
        return string.Format("{0:0000}/{1:00}/{2:00}", jc.GetYear(date), 
               jc.GetMonth(date), jc.GetDayOfMonth(date));
      }

}

The extension method GetPersianDate needs to be defined in a static class. You can refactor like this:

public partial class bVoteAnswer : System.Web.UI.Page
{
    UserVotes ue = new UserVotes();
    bVotes bv = new bVotes();
    bVoteAnswers ve = new bVoteAnswers();
    public void Page_Load(object sender, EventArgs e)
    {

        dropdownlist.Enabled = false;
        int BuildingId = Convert.ToInt32(Session["BuildingId"]);
        DataTable dt = new DataTable();
        dt = bv.Select(0, "", "", "", BuildingId, "",0);
        Grid_Vote.DataSource = dt;
        Grid_Vote.DataBind();

    }

    protected void Grid_Vote_RowCreated(object sender, GridViewRowEventArgs e)
    {
        e.Row.Cells[0].Visible = false;
    }

    protected void Grid_Vote_SelectedIndexChanged(object sender, EventArgs e)
    {
        dropdownlist.Enabled = true;
        int VoteId = Convert.ToInt32(Grid_Vote.SelectedRow.Cells[0].Text);
        DataTable dt = new DataTable();
        dt = ve.Select(0,VoteId,"");
        for (int i = 0; i <= dt.Rows.Count; i++)
        {
            ListItem l = new ListItem(dt.Rows[i][2].ToString(), dt.Rows[i][2].ToString());
            dropdownlist.Items.Add(l);
        }
    }

    protected void btn_insert_Click(object sender, EventArgs e)
    {
        int OwnerId = Convert.ToInt32(Session["OwnerId"]);
        int VoteId = Convert.ToInt32(Grid_Vote.SelectedRow.Cells[0].Text);
        DataTable dt = new DataTable();
        dt = ve.Select(0, VoteId, dropdownlist.SelectedValue);
        int AnswerId = Convert.ToInt32(dt.Rows[0][0]);
        DateTime ClientDateTime = DateTime.Now;
        string PersianDate = GetPersianDate(ClientDateTime);
        ue.Insert(0, VoteId, OwnerId, AnswerId, PersianDate);
    }
}

public static class PersionCalendarExtension
{
    public static string GetPersianDate(this DateTime date)
    {
        PersianCalendar jc = new PersianCalendar();
        return string.Format("{0:0000}/{1:00}/{2:00}", jc.GetYear(date), jc.GetMonth(date), jc.GetDayOfMonth(date));
    }
}

Remove the 'this' from

GetPersianDate(this DateTime date)

You're not using it as an extension method anyway, which would enable you to write eg

DateTime.Now.GetPersianDate()

You can read more about extension methods here: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM