私は私のasp.net MVCクラス内の以下のメソッドがあります: -オブジェクト参照が非静的フィールド、メソッド、またはプロパティのために必要とされる
public class Crud
{
Repository repository = new Repository();
List<string> activeTech = repository.getAll().ToList();
が、私は次の例外を取得しています: -
を次のように私のコードに静的追加する場合Error 1 An object reference is required for the non-static field, method, or property
はしかし、例外が削除されます: -
public class Crud {
static Repository repository = new Repository();
List<string> activeTech = repository.getAll().ToList();
ので、CAこれについて誰も助言してくれませんか?あなたのコンストラクタのコードが実行される前に、クラス内のフィールドの
おかげ
2番目のステートメントがメソッド内にあることを確認しますか? –