【问题标题】:Search for null objects inside array of objects using LINQ使用 LINQ 在对象数组中搜索空对象
【发布时间】: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事件中移动数组初始化

标签: c# vb.net winforms linq


【解决方案1】:

(对于未来的观众)

根据@Ivan Stoev cmets 的说法,问题已解决。
“因为字段初始值设定项在类构造函数之前运行”。
或者换句话说,我的数组是在初始化对象之前声明的。

在初始化后将 NumericUpdowns 添加到数组中(在调用 InitializeComponent() 之后的构造函数中)就可以了..
也就是总结的固定代码:

Public Class PerformanceForm

    Sub New()
        ' This call is required by the designer. '
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call. '
        PrimaryWeightNumsAr = {num_Primary_Billing, num_Primary_Rutine, num_Primary_Seker, num_Primary_Sla}
    End Sub
    Private PrimaryWeightNumsAr() As NumericUpDown

    Private Sub PrimaryWeightsValueChanged()

        If PrimaryWeightNumsAr Is Nothing Then Exit Sub
        ' do other stuff... '

    End Sub

End Class

C# 版本:

public class PerformanceForm
{

    public PerformanceForm()
    {
        // This call is required by the designer. 
        InitializeComponent();
        // Add any initialization after the InitializeComponent() call. 
        PrimaryWeightNumsAr = {
            num_Primary_Billing,
            num_Primary_Rutine,
            num_Primary_Seker,
            num_Primary_Sla
        };
    }

    private NumericUpDown[] PrimaryWeightNumsAr;
    private void PrimaryWeightsValueChanged()
    {

        if (PrimaryWeightNumsAr == null)
            return;
        // do other stuff... 

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    相关资源
    最近更新 更多