代码之家  ›  专栏  ›  技术社区  ›  Scott

需要VB.NET列表逻辑的帮助吗

  •  0
  • Scott  · 技术社区  · 14 年前

    嘿,伙计们,所以我正在创建一个列表(字符串),大小总是9。

    此列表包含真/假值。我需要遍历这个列表,找到3个为真的值(永远不会超过3,但可以小于3),然后在代码中将3个字符串值设置为这些值的3个索引+1。

    Private Sub SetDenialReasons(ByVal LoanData As DataRow)
            Dim reasons As New List(Of String)
            With reasons
                .Add(LoanData.Item("IsDenialReasonDTI").ToString)
                .Add(LoanData.Item("IsDenialReasonEmploymentHistory").ToString)
                .Add(LoanData.Item("IsDenialReasonCreditHistory").ToString)
                .Add(LoanData.Item("IsDenialReasonCollateral").ToString)
                .Add(LoanData.Item("IsDenialReasonCash").ToString)
                .Add(LoanData.Item("IsDenialReasonInverifiableInfo").ToString)
                .Add(LoanData.Item("IsDenialReasonIncomplete").ToString)
                .Add(LoanData.Item("IsDenialReasonMortgageInsuranceDenied").ToString)
                .Add(LoanData.Item("IsDenialReasonOther").ToString)
            End With
    
            Dim count As Integer = 0
            For Each item As String In reasons
                If item = "True" Then
                    count += 1
                End If
            Next
    
            If count = 1 Then
                DenialReason1 = (reasons.IndexOf("True") + 1).ToString
            ElseIf count = 2 Then
                DenialReason1 = (reasons.IndexOf("True") + 1).ToString
                DenialReason2 = (reasons.LastIndexOf("True") + 1).ToString
            ElseIf count >= 3 Then
                Dim tempIndex As Integer = reasons.IndexOf("True")
                DenialReason1 = (reasons.IndexOf("True") + 1).ToString
                DenialReason2 = (reasons.IndexOf("True", tempIndex, reasons.Count - 1) + 1).ToString
                DenialReason3 = (reasons.LastIndexOf("True") + 1).ToString
            End If
        End Sub
    

    我在数组中有3个True相邻,代码失败了,异常是count必须是正数或者别的什么。

    现在,如果有少于3个真的,它应该设置其余的DenialReason的,还没有被设置为空白(但是他们被设置为空白,在构造函数中已经考虑到这一点)。

    有什么想法吗?

    1 回复  |  直到 14 年前
        1
  •  1
  •   Ben McCormack    14 年前

    For Each 用于处理 DenialReason s。这仍然感觉像一个黑客,但我认为它可能会更干净,你所拥有的。如果使用此代码,则不需要以开头的代码 If count = 1... :

        Dim count As Integer = 0
        Dim index As Integer = 1
        For Each item As String In reasons
            If item = "True" Then
                count += 1
                Select Case count
                    Case 1
                        DenialReason1 = index.ToString()
                    Case 2
                        DenialReason2 = index.ToString()
                    Case 3
                        DenialReason3 = index.ToString()
                End Select
            End If
            index += 1
        Next
    

    index IndexOf() .

    我认为一个更好的解决办法是列出 拒绝理由 s并添加到该列表中,因为项为真:

        Dim count As Integer = 0
        Dim index As Integer = 1
        Dim denialReasons As New List(Of String)()
        For Each item As String In reasons
            If item = "True" Then
                denialReasons.Add(index)
            End If
            index += 1
        Next
    

    然后您可以简单地遍历 denialReasons . 这是灵活的,所以无论出于什么原因,如果你有三个以上 拒绝理由