简体   繁体   中英

How can I make this C# class and method static?

I am a new ASP.NET developer and I am trying to learn Linq-To-Entities. I am trying to create a static class called Items in my Data Access Layer. This class has a method to retrieve all the records in Items Entity. And then, I will use it for binding the GridView to it. The problem is that I got the following error in my getData() method when I make that class as a static class and I don't know why:

在此输入图像描述

C# Code:

public static class Items
{
    //properties
    public int ID { get; set; }
    public string Code { get; set; }
    public int ItemTypeID { get; set; }
    public string Name { get; set; }
    public int StatusID { get; set; }


    public static IEnumerable<Items> getData()
    {
        List<Items> itemsList = new List<Items>();
        using (ItemsDBEntities context = new ItemsDBEntities())
        {
            itemsList = (from item in context.Items
                        select new Items()
                        {
                            ID = item.ID,
                            Code = item.Code,
                            Name = item.Name,
                            StatusID = item.StatusID
                        }).ToList();
        }
        return itemsList;
    }

Could you please tell me how to fix this error?

Could you please tell me how to fix this error?

//public static class Items
public class Items
{
}

There are clear reasons why Items should not be static .

The getData() method can remain static, although that doesn't seem necessary or useful either. It just makes testing a little harder. Do research state-management and the use of static in ASP.NET.

  • do ask yourself why you think it needs to be static.
  • anything with Id and Name properties is an Item , not an Items .
  • in C# we capitalize methods, GetData() .

只需删除Items类的静态声明即可。

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