我有2個現有函數。一個我不能編輯(getServiceNames),第二個我需要設置一個字符串(getDataTableOne)。 我想用同一個類中的函數設置這個字符串(getAllExceptNiServiceNames),但是他給了我這個錯誤,因爲我想編輯的函數是靜態的。對象引用是必需的非靜態字段,方法或屬性錯誤
內的對象引用所需的非靜態字段,方法或屬性 「Queries.getAllExceptNiServiceNames()」
我不能去除功能的靜態屬性和我還可以」創建一個字符串對象。 最好的解決辦法是什麼?
public static DataSet getDataTableOne()
{
string serviceNameFilterLineThree = getAllExceptNiServiceNames(); //ERROR
}
public static DataSet getServiceNames()
{
DataSet ds = new DataSet();
string query_select = "select test";
ds = QualityGate.fillDataset(query_select);
return ds;
}
public string getAllExceptNiServiceNames()
{
string sql = "";
DataSet ds = getServiceNames();
int i = 0;
foreach (DataRow theRow in ds.Tables[0].Rows)
{
if (i != 0)
sql += "AND ";
sql += "serviceName = '" + theRow["serviceName"].ToString() + "' ";
i++;
}
return sql;
}
@Dan:我同意這個方法需要是靜態的,但是你包含的代碼沒有'static'關鍵字! –
感謝它的工作原理,如果我只是把這個功能做成靜態的,那就足夠了。 – Freddy