次の方法で共有


コンパイラ エラー CS0038

更新 : 2007 年 11 月

エラー メッセージ

入れ子にされた型 'type2' を経由して、外の型 'type1' の static でないメンバにアクセスすることはできません。

クラスに含まれるフィールドが、入れ子になったクラスで自動的に使用可能になることはありません。入れ子になったクラスで使用可能にするには、フィールドが静的であることが必要です。それ以外の場合は、外部クラスのインスタンスを作成する必要があります。詳細については、「入れ子にされた型 (C# プログラミング ガイド)」を参照してください。

次の例では CS0038 エラーが生成されます。

// CS0038.cs
class OuterClass
{
   public int count;
   // try the following line instead
   // public static int count;

   class InnerClass
   {
      void func()
      {
         // or, create an instance
         // OuterClass class_inst = new OuterClass();
         // int count2 = class_inst.count;
         int count2 = count;   // CS0038
      }
   }

   public static void Main()
   {
   }
}