我找不到任何解決方案張貼問題:錯誤:字段初始不能引用非靜態字段,方法或屬性
class ApiData
{ SqlCeConnection conn = new SqlCeConnection(@"Data Source=C:\Users\Peter\Documents \db.sdf;");
SqlCeCommand cmd = null;
SqlCeDataReader rdr = null;
public string code()
{
conn.Open();
cmd = conn.CreateCommand();
cmd.CommandText ="SELECT code FROM Charakter WHERE id=1";
rdr = cmd.ExecuteReader();
rdr.Read();
string selected = rdr.GetString(0);
conn.Close();
return (selected);
}
class Data{
ApiData g= new ApiData();
string vode = **g.code();**
}
這是爲什麼收到錯誤「A字段初始不能引用非靜態字段,方法或屬性「在粗體標記行?
重複的http://stackoverflow.com/questions/7400677/a-field-initializer-can-reference-the-non-static-field-method-or-property – zebrabox