【发布时间】:2017-07-22 09:55:35
【问题描述】:
我确信这很容易,但我无法弄清楚,也没有找到答案,
我正在检查 NumericUpDown 数组中的对象是否为空
(PrimaryWeightsValueChanged() 由 valueChange 事件调用,在表单加载之前它们应该为 null)。
但问题是即使在控件初始化并具有值之后条件总是返回 true
我做错了什么?
private NumericUpDown[] PrimaryWeightNumsAr = {
num_Primary_Billing,
num_Primary_Rutine,
num_Primary_Seker,
num_Primary_Sla
};
private void PrimaryWeightsValueChanged()
{
// even when NumericUpDown`s are not null it enters here
if (PrimaryWeightNumsAr.AsEnumerable().Any(x => x == null))
return;
// doing stuff when not null...
}
视觉基础版:
Private PrimaryWeightNumsAr() As NumericUpDown = {num_Primary_Billing, num_Primary_Rutine, num_Primary_Seker, num_Primary_Sla}
Private Sub PrimaryWeightsValueChanged()
' even when NumericUpDown`s are not null it enters here '
If PrimaryWeightNumsAr.AsEnumerable().Any(Function(x) x Is Nothing) Then Exit Sub
If PrimaryWeightNumsAr.AsEnumerable().Sum(Function(x) x.Value) <> 100 Then
For Each itm As NumericUpDown In PrimaryWeightNumsAr
itm.BackColor = GuiProfile.sys_red
Next
Else
For Each itm As NumericUpDown In PrimaryWeightNumsAr
itm.BackColor = Color.SpringGreen
Next
End If
End Sub
【问题讨论】:
-
您到底需要什么?如果您只需要检查 null 的存在,那么您有正确的答案。
-
@YuriyN 我也在用visual basic.net,我会添加vb版本也许会更清楚
-
我敢肯定,你不需要演员
AsEnumerable,因为数组已经实现了它。但是您在 C# 中的代码与 VB 完全一样。我看到它检查数组中的任何元素是否为空,但数组中的所有元素都已初始化,它们不能为空。 -
@jonathana 但是它们是在构造函数调用的
InitializeComponent中初始化的,对吧?请参阅我之前的评论。 -
然后在构造函数中(在
InitializeComponent调用之后)或Load事件中移动数组初始化