1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

C# error: A field initializer cannot reference the nonstatic field, method, or property

Last updated at Posted at 2019-06-12

解消方法

今回でたエラー文は以下のもの,
A field initializer cannot reference the nonstatic field, method, or property `原因部分`

Google先生に翻訳していただくと,
フィールド初期化子は、非静的フィールド、メソッド、またはプロパティを参照できません
との意味だそうです.
エラーがでたコードはこちらになります,

エラーコード

using System;

public class Product
{
	public int Code { get; private set; }
	public string Name { get; private set; }
	public int Price { get; private set; }

	public Product(int code, string name, int price)
	{
		this.Code = code;
		this.Name = name;
		this.Price = price;
	}

	public int GetTax()
	{
		return (int)(Price * 0.08);
	}

	public int GetPriceIncludingTax()
	{
		return Price + GetTax();
	}

	Product karinto = new Product(123, "かりんとう", 180);
	Product daihuku = new Product(235, "大福餅", 160);
	int karintoTax = karinto.GetTax();
	int daihukuTax = daihuku.GetTax();
	static void Main(string[] args)
	{
		//code
	}
}

コンパイル結果は以下のものです,

結果
$ mcs Chapter1.cs
Chapter1.cs(28,19): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `Product.karinto'
Chapter1.cs(29,19): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `Product.daihuku'
Compilation failed: 2 error(s), 0 warnings

エラー文からわかる通り, Product.karintoProduct.daihukuに問題があるようです.
では, どのように解決するのか.
karinto と daihuku の二つの変数をstaticにするだけです.
以下は, 修正コードです.

修正コード
using System;

public class Product
{
	public int Code { get; private set; }
	public string Name { get; private set; }
	public int Price { get; private set; }

	public Product(int code, string name, int price)
	{
		this.Code = code;
		this.Name = name;
		this.Price = price;
	}

	public int GetTax()
	{
		return (int)(Price * 0.08);
	}

	public int GetPriceIncludingTax()
	{
		return Price + GetTax();
	}

	//ここを変更した↓
	static Product karinto = new Product(123, "かりんとう", 180);
	static Product daihuku = new Product(235, "大福餅", 160);
	int karintoTax = karinto.GetTax();
	int daihukuTax = daihuku.GetTax();
	static void Main(string[] args)
	{
		//code
	}
}

すると, 先ほどまでのエラー文はなくなります.
staticフィールド内で宣言する場合はそもそもstatic演算子は不必要になります.
以下に例を示します.

using System;

namespace SampleApp
{
	class Product
	{
		public int Code { get; private set; }
		public string Name { get; private set; }
		public int Price { get; private set; }

		public Product(int code, string name, int price)
		{
			this.Code = code;
			this.Name = name;
			this.Price = price;
		}

		public int GetTax()
		{
			return (int)(Price * 0.08);
		}

		public int GetPriceIncludingTax()
		{
			return Price + GetTax();
		}
	}
	class Program
	{
		static void Main(string[] args)
		{
			Product karinto = new Product(123, "かりんとう", 180);
			int taxIncluded = karinto.GetPriceIncludingTax();
			Console.WriteLine(taxIncluded);
		}
	}
}

このコードでもエラーは出ません.

最後に

未来の自分用に書いたので雑だったら申し訳ありません.

参考文献

実戦で役立つ C#プログラミングのイディオム/定石&パターン (最終閲覧:2019/6/11)
万年素人からGeekへの道  (最終閲覧:2019/6/11)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?