How do I combine 3 for a loop?

I have 3 for each statement that loops through the statutes in the XML document ( aobjXMLCaseDoc ) and then checks if those statutes are in line with the following statutes 260C.405, 518.131, 518B.01, 609.748, 629.75

I would like to combine these 3 for each into one for each statement. How to do it?

XML

<?xml version="1.0" encoding="UTF-8"?>
<Charge>
    <ChargeHistory ChargeHistoryID="387">
        <Statute>
            <StatuteNumber>609.352.2a(2)</StatuteNumber>
        </Statute>
    </ChargeHistory>
    <ChargeHistory ChargeHistoryID="398">
        <Statute>
            <StatuteNumber>609.352.2a(2)</StatuteNumber>
        </Statute>
    </ChargeHistory>
    <ChargeHistory ChargeHistoryID="517">
        <Statute>
            <StatuteNumber>609.352.2a(2)</StatuteNumber>
        </Statute>
    </ChargeHistory>
    <Deleted>false</Deleted>
</Charge>

      

VB code

'Check if statute has 8 characters
    For Each objXMLStatuteNode In aobjXMLCaseDoc.DocumentElement.SelectNodes("Case/Charge/ChargeHistory/Statute/StatuteNumber")
        If objXMLStatuteNode.InnerText.Length >= 8 Then
            strStatuteEightDigits = objXMLStatuteNode.InnerText.Substring(0, 8)
            Select Case strStatuteEightDigits
                Case "260C.405"
                    blnNotSpecifiedStatute = False
                Case Else
                    blnNotSpecifiedStatute = True
            End Select
        End If
    Next

    'Check if statute has 7 characters 
    For Each objXMLStatuteNode In aobjXMLCaseDoc.DocumentElement.SelectNodes("Case/Charge/ChargeHistory/Statute/StatuteNumber")
        If objXMLStatuteNode.InnerText.Length >= 7 Then
            strStatuteSevenDigits = objXMLStatuteNode.InnerText.Substring(0, 7)
            Select Case strStatuteSevenDigits
                Case "518.131", "518B.01", "609.748"
                    blnNotSpecifiedStatute = False
                Case Else
                    blnNotSpecifiedStatute = True
            End Select
        End If
    Next

    'Check if statute has 6 characters 
    For Each objXMLStatuteNode In aobjXMLCaseDoc.DocumentElement.SelectNodes("Case/Charge/ChargeHistory/Statute/StatuteNumber")
        If objXMLStatuteNode.InnerText.Length >= 6 Then
            strStatuteSixDigits = objXMLStatuteNode.InnerText.Substring(0, 6)
            Select Case strStatuteSixDigits
                Case "629.75"
                    blnNotSpecifiedStatute = False
                Case Else
                    blnNotSpecifiedStatute = True
            End Select
        End If
    Next

      

+3


source to share


2 answers


All your code seems to be checking to see if any of the begin stat numbers are specified with one of the five lines ("260C.405", "518.131", "518B.01"), "609,748", "629,75" ). You can use StartsWith

for this.

You can use Any

to check if a certain status number matches some test, and this test can also be used Any

to see if a status number starts with any of the list of strings. All of this can be done in one application.

The XML example you provide starts with <Charge>

, but your code seems to assume that the elements <Charge>

exist on the element <Case>

. To be consistent I have removed <Case>

from the code. You will need to put it back (just before <Charge>

) if needed.



Note that your code (and mine below) checks for any status number starting with "629.75", so "629.751" will be considered a match. Perhaps you want to check the statute numbers starting with "629.75". not "629.75"

Private statutes As New List(Of String) From {"260C.405", "518.131", "518B.01", "609.748", "629.75"}
Private objXML As XDocument = <?xml version="1.0" encoding="UTF-8"?>
                              <Charge>
                                  <ChargeHistory ChargeHistoryID="387">
                                      <Statute>
                                          <StatuteNumber>609.352.2a(2)</StatuteNumber>
                                      </Statute>
                                  </ChargeHistory>
                                  <ChargeHistory ChargeHistoryID="398">
                                      <Statute>
                                          <StatuteNumber>609.352.2a(2)</StatuteNumber>
                                      </Statute>
                                  </ChargeHistory>
                                  <ChargeHistory ChargeHistoryID="517">
                                      <Statute>
                                          <StatuteNumber>609.352.2a(2)</StatuteNumber>
                                      </Statute>
                                  </ChargeHistory>
                                  <Deleted>false</Deleted>
                              </Charge>

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim blnNotSpecifiedStatute As Boolean
    blnNotSpecifiedStatute = Not objXML.<Charge>.<ChargeHistory>.<Statute>.<StatuteNumber>.Any(Function(n) statutes.Any(Function(s) n.Value.StartsWith(s)))
    'Do something with blnNotSpecifiedStatute 
End Sub

      

+2


source


Use If-ElseIf:

For Each objXMLStatuteNode In aobjXMLCaseDoc.DocumentElement....

    If objXMLStatuteNode.InnerText.Length >= 8 Then  
       ''' Checks for 8 or more chars
       ....                     
    ElseIf objXMLStatuteNode.InnerText.Length = 7 Then  
       ''' Checks for 7 Chars
       .... 
    ElseIf objXMLStatuteNode.InnerText.Length = 6 Then   
       ''' Checks for 6 chars
       ....
    Else
       ''' For Less than 6 chars
    End If

Next

      



Also, I think the logic might need a little tweaking because 8 -> 8, but it's also> = 7 and also> = 6.

+1


source







All Articles