使用两个" IsNot Nothing"在单个If语句中

时间:2018-02-18 10:16:23

标签: vb.net

需要: 我需要在IF条件下检查其中任何一个条件是否为真。我想检查DrainLeftFound或DrainRightFound是否为空。如果不满足这两个条件,则条件应该失败,但如果它们中的任何一个为真,则该条件应该存在。如果任一条件为真,我需要计算Total FoundationLength,但如果个别条件为真,则总计添加FoundationArea。我尝试使用IsNot Nothing但存在以下问题。 - 当第一个条件为空但第二个条件不为空时,它    工作正常   - 当两个条件都不为空时它会起作用   - 但它一直有效,直到两个条件都不为空,当第一个为空而第二个不为空时停止工作。

    Dim FoundationLength As Double, FoundationArea As Double
    For Each Area As DrainArea In Mydrainareacalc.MyDrainarea.Values
        'If (Area.DrainLeftFound IsNot Nothing) Or (Area.DrainRightFound IsNot Nothing) Then
        If ((Area.DrainRightFound IsNot Nothing) OrElse (Area.DrainRightFound IsNot Nothing)) Then
            FoundationLength += Area.Length
            If Area.DrainLeftFound IsNot Nothing Then
                For Each LeftFound As FoundLayerDetail In Area.DrainLeftFound
                    FoundationArea += LeftFound.LayerArea
                Next
            End If
            If Area.DrainRightFound IsNot Nothing Then
                For Each RightFound As FoundLayerDetail In Area.DrainRightFound
                    FoundationArea += RightFound.LayerArea
                Next
            End If
        End If
    Next

被修改 我想检查是否存在其中一个条件然后执行循环。 在特定条件

中,代码未能达到上述条件中的第三点
If ((Area.DrainRightFound IsNot Nothing) OrElse (Area.DrainRightFound IsNot Nothing)) Then

2 个答案:

答案 0 :(得分:0)

DrainLeftFound和DrainRightFound是什么数据类型?从名称来看,它听起来像一个布尔值。对象区域只能是属性DrainLeftFound和DrainRightFound将具有一个值,即使尚未分配一个值。 (价值类型)

Public Class AutomaticInitialization
    Private Sub AutomaticInitialization_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim da As New DrainArea
        Debug.Print(da.DrainLeftFound.ToString)
        Debug.Print(da.DrainRightFound.ToString)
    End Sub
End Class
Public Class DrainArea
    Public DrainLeftFound As Boolean
    Public DrainRightFound As Boolean
End Class

答案 1 :(得分:0)

为方便起见,我删除了第一个带有OrElse条件的循环,定义了单个行的长度变量,以便我可以稍后加总来查找总长度。 删除:

If ((Area.DrainRightFound IsNot Nothing) OrElse (Area.DrainRightFound IsNot Nothing)) Then

请按照以下步骤操作。


Dim FoundationLength As Double, FoundationArea As Double, **IndividualLength As Double**
                            For Each Area As DrainArea In Mydrainareacalc.MyDrainarea.Values
                                **IndividualLength = 0**
                                If Area.DrainLeftFound IsNot Nothing Then
                                    IndividualLength = Area.Length
                                    For Each LeftFound As FoundLayerDetail In Area.DrainLeftFound
                                        FoundationArea += LeftFound.LayerArea
                                    Next
                                End If
                                If Area.DrainRightFound IsNot Nothing Then
                                    IndividualLength = Area.Length
                                    For Each RightFound As FoundLayerDetail In Area.DrainRightFound
                                        FoundationArea += RightFound.LayerArea
                                    Next
                                End If
                                **FoundationLength += IndividualLength**

                            Next