字段初始值设定项不能引用非静态字段、方法或属性 [英] A field initializer cannot reference the nonstatic field, method, or property
问题描述
我有一个课程,当我尝试在另一个课程中使用它时,我收到以下错误.
使用系统;使用 System.Collections.Generic;使用 System.Linq;命名空间 MySite{公开课提醒{公共字典<TimeSpan, string>时间跨度文本 { 获取;放;}//我们正在使用 Costructor 设置默认值公开提醒(){TimeSpanText.Add(TimeSpan.Zero, "None");TimeSpanText.Add(new TimeSpan(0, 0, 5, 0), "5 分钟前");TimeSpanText.Add(new TimeSpan(0, 0, 15, 0), "15 分钟前");TimeSpanText.Add(new TimeSpan(0, 0, 30, 0), "30 分钟前");TimeSpanText.Add(new TimeSpan(0, 1, 0, 0), "1 小时前");TimeSpanText.Add(new TimeSpan(0, 2, 0, 0), "2 小时前");TimeSpanText.Add(new TimeSpan(1, 0, 0, 0), "1 天前");TimeSpanText.Add(new TimeSpan(2, 0, 0, 0), "2 天前");}}}
在另一个类中使用该类
class SomeOtherClass{私人提醒提醒 = new Reminders();//错误发生在这一行:私人动态 defaultReminder = 提醒.TimeSpanText[TimeSpan.FromMinutes(15)];....
错误(CS0236):
字段初始值设定项不能引用非静态字段、方法或属性
为什么会发生这种情况以及如何解决?
这一行:
private dynamic defaultReminder =提醒.TimeSpanText[TimeSpan.FromMinutes(15)];
您不能使用实例变量来初始化另一个实例变量.为什么?因为编译器可以重新排列这些 - 不能保证 reminder
会在 defaultReminder
之前被初始化,所以上面的行可能抛出一个 NullReferenceException
.
相反,只需使用:
private dynamic defaultReminder = TimeSpan.FromMinutes(15);
或者,在构造函数中设置值:
私有动态默认提醒;公开提醒(){defaultReminder = prompt.TimeSpanText[TimeSpan.FromMinutes(15)];}
MSDN 上有关于此编译器错误的更多详细信息 - 编译器错误 CS0236.>
I have a class and when I try to use it in another class I receive the error below.
using System;
using System.Collections.Generic;
using System.Linq;
namespace MySite
{
public class Reminders
{
public Dictionary<TimeSpan, string> TimeSpanText { get; set; }
// We are setting the default values using the Costructor
public Reminders()
{
TimeSpanText.Add(TimeSpan.Zero, "None");
TimeSpanText.Add(new TimeSpan(0, 0, 5, 0), "5 minutes before");
TimeSpanText.Add(new TimeSpan(0, 0, 15, 0), "15 minutes before");
TimeSpanText.Add(new TimeSpan(0, 0, 30, 0), "30 minutes before");
TimeSpanText.Add(new TimeSpan(0, 1, 0, 0), "1 hour before");
TimeSpanText.Add(new TimeSpan(0, 2, 0, 0), "2 hours before");
TimeSpanText.Add(new TimeSpan(1, 0, 0, 0), "1 day before");
TimeSpanText.Add(new TimeSpan(2, 0, 0, 0), "2 day before");
}
}
}
Using the class in another class
class SomeOtherClass
{
private Reminders reminder = new Reminders();
// error happens on this line:
private dynamic defaultReminder = reminder.TimeSpanText[TimeSpan.FromMinutes(15)];
....
Error (CS0236):
A field initializer cannot reference the nonstatic field, method, or property
Why does it happen and how to fix it?
This line:
private dynamic defaultReminder =
reminder.TimeSpanText[TimeSpan.FromMinutes(15)];
You cannot use an instance variable to initialize another instance variable. Why? Because the compiler can rearrange these - there is no guarantee that reminder
will be initialized before defaultReminder
, so the above line might throw a NullReferenceException
.
Instead, just use:
private dynamic defaultReminder = TimeSpan.FromMinutes(15);
Alternatively, set up the value in the constructor:
private dynamic defaultReminder;
public Reminders()
{
defaultReminder = reminder.TimeSpanText[TimeSpan.FromMinutes(15)];
}
There are more details about this compiler error on MSDN - Compiler Error CS0236.
这篇关于字段初始值设定项不能引用非静态字段、方法或属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!