【发布时间】:2018-03-19 23:52:24
【问题描述】:
所以基本上我想检查我的 C# 应用程序中是否设置了设置。代码在这里
if (Default["StudentAccountTypeDefault"] is null) // Crashes after this
{
//
}
else
{
//
}
似乎在空检查时崩溃了。我在那里放了一个断点,它显示Default["DefaultStudentAccountType"] 只是一个空白字符串。为什么它会因 NullReferenceException 而崩溃?我很确定这就是它崩溃的地方——如果我注释掉 if 语句,它会按预期工作。
编辑:为了减轻一些混乱。太好了,Default 实际上是 Settings.Default,此外,我实际上是在尝试在 Settings() 构造函数中访问它。所以,很明显,在它被初始化之前。哎呀。 “完整”-er 代码如下。
public Settings() {
// // To add event handlers for saving and changing settings, uncomment the lines below:
//
// this.SettingChanging += this.SettingChangingEventHandler;
//
// this.SettingsSaving += this.SettingsSavingEventHandler;
//
if (Settings.Default["DefaultStudentAccountType"] is null)
{
}
else
{
}
}
【问题讨论】:
-
它的
Settings.Default。我试图在构造函数中访问它,这是一个愚蠢的想法。