简体   繁体   English

asp.net c# - 字段初始值设定项不能引用非静态字段,方法或属性

[英]asp.net c# - A field initializer cannot reference the non-static field, method or property

I have the following code in my class: 我班上有以下代码:

    public string StrLog {get; set;}

then within the same class, I have the following code: 然后在同一个类中,我有以下代码:

    private string imgData = StrLog;

I get the following error message: 我收到以下错误消息:

    A field initializer cannot reference the non-static field, method or property

It has a problem with: 它有一个问题:

    private string imgData = StrLog;

but not sure how to resolve this. 但不知道如何解决这个问题。

Basically, you cannot initialise a class level variable by using any other class level value (unless that value is static) - which is what your error is trying to tell you. 基本上,您不能使用任何其他类级别值初始化类级别变量(除非该值是静态的) - 这是您的错误试图告诉您的。

Your best option would be to assign the value in your constructor: 您最好的选择是在构造函数中分配值:

private string imgData = null;

public MyClass()
{
   imgData = "some value";
}

In your case there is no point assigning it the value of StrLog because StrLog won't have a value to begin with. 在你的情况下,没有必要为它分配StrLog的值,因为StrLog没有值开始。 So you may as well just assign it null , or an actual value form somewhere else (like my example) 所以你也可以把它分配给null ,或者在其他地方分配一个实际的值(比如我的例子)

You are not allowed to use a non-static memeber to intialize a member variable. 您不能使用非静态memeber初始化成员变量。

You need to intialize it first by setting it up in your constructor. 您需要首先通过在构造函数中设置它来初始化它。

eg: 例如:

imgData = null;

I would strongly encourage you to assign something (something could be null) in the constructor. 我强烈建议你在构造函数中指定一些东西 (可能是null)。 It's just good form. 这只是一种好形式。 In the example below, you will see why it is important. 在下面的示例中,您将了解它为何如此重要。 What if a get is executed first and the value isn't set? 如果首先执行get并且未设置值,该怎么办? It should at least contain a null value. 它应该至少包含一个空值。

Having said that, if you want the value of imgData to be populated with the value of your public-facing property, you need to do the following: 话虽如此,如果您希望使用面向公众的属性的值填充imgData的值,则需要执行以下操作:

public string StrLog
{
   get { return imgData; }
   set { imgData = value; }
}

This will pass the value of StrLog into imgData, with no work required on your part. 这会将StrLog的值传递给imgData,而您无需任何工作。

Make imgData your property , same way as Strlog. 以与Strlog相同的方式使imgData成为您的属性。 and then assign . 然后分配。 it will work. 它会工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 字段初始化程序无法引用ASP.Net MVC Controller中的非静态字段,方法或属性 - A field initializer cannot reference the non-static field, method, or property in ASP.Net MVC Controller 字段初始值设定项不能引用非静态字段,方法或属性吗? .1 - A field initializer cannot reference the non-static field, method, or property? .1 字段初始值设定项不能引用非静态字段、方法或属性? - A field initializer cannot reference the non-static field, method, or property? 字段初始值设定项无法引用非静态字段,方法或属性5 - A field initializer cannot reference the non-static field, method, or property 5 字段初始值设定项不能引用非静态字段,方法或属性 - A field initializer cannot reference the non-static field, method, or property 字段初始值设定项不能引用非静态字段,方法或属性 - A field initializer cannot reference the non-static field, method or property 字段初始值设定项不能引用非静态字段,方法或属性 - A field initializer cannot reference the non-static field, method, or property 字段初始值设定项不能引用非静态字段方法或属性 - A field initializer cannot reference the non-static field method or property 属性初始化程序无法引用非静态字段 - Property Initializer cannot reference non-static field 遇到错误:字段初始化程序无法引用非静态字段,方法或属性 - Getting error: A field initializer cannot reference the non-static field, method, or property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM