简体   繁体   English

如何检查Properties.Settings.Default。 <field> int类型的值在C#中不为null或为空

[英]How to check that Properties.Settings.Default.<field> of an int type is not null or empty in c#

I have a Properties.Settings.Default.DateRetentionInDays field in my settings/config file for ac# console app. 我的ac#控制台应用程序的设置/配置文件中有一个Properties.Settings.Default.DateRetentionInDays字段。 It is an int type. 这是一个int类型。 If that field is null, the program crashes with an error indicating that I need to check to see if it is null before calling the method. 如果该字段为null,则程序崩溃并显示错误,指示我需要在调用该方法之前检查它是否为null。 I've tried a few ways to check if it is null, but none have worked. 我尝试了几种方法来检查它是否为空,但没有一个起作用。

I know how to do this for a string data type, and see some useful things to try for int data type for java, but not seeing the solution for an int data type in c#. 我知道如何对字符串数据类型执行此操作,并且看到了一些有用的东西来尝试Java的int数据类型,但是没有看到c#中的int数据类型的解决方案。

I tried the following because not only should the field not be null, it also needs to be < than 0, but it failed with the same error: if (Properties.Settings.Default.DateRetentionInDays >= 0) . 我尝试了以下操作,因为该字段不仅应该不为null,还必须小于0,但失败并出现相同的错误: if (Properties.Settings.Default.DateRetentionInDays >= 0)

Open your settings in the designer and make sure Value column for the DateRetentionInDays property is not empty. 在设计器中打开设置,并确保DateRetentionInDays属性的“ Value列不为空。 You need to put some number there (presumably valid) to serve as default value for the property. 您需要在此放置一些数字(可能有效)作为该属性的默认值。

There is no other way because the code is auto generated. 没有其他方法,因为代码是自动生成的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM