无法使用实例引用访问成员<method>;使用类型名称来限定它</method>

时间:2015-03-11 19:30:07

标签: c# class

班级档案:

public class ServiceAccess
{
    public DataTable GetAllShortCodes()
    {
        DataTable ShortCodeTable = new DataTable();
        // logic to add data to Datatable
        return ShortCodeTable;
    }
}

代码背后:

    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = ServiceAccess.GetAllShortCodes();
    }

很长一段时间都在做VB,但对C#来说是新手。我做错了什么?

1 个答案:

答案 0 :(得分:3)

您正在调用GetAllShortCodes,就像静态方法一样

您需要将其更改为此

var service = new ServiceAccess();
DataTable dt = service.GetAllShortCodes();

或将方法更改为静态方法

public static DataTable GetAllShortCodes()
{
    DataTable ShortCodeTable = new DataTable();
    // logic to add data to Datatable
    return ShortCodeTable;
}