嘿伙计们,所以我正在创建一个List(Of String),总是大小为9。
此列表包含True / False值。我需要浏览这个列表并找到3个值为True(永远不会超过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,但代码失败,并且异常表示计数必须是正数或其他。
现在如果少于3个True,它应该将尚未设置的剩余DenialReason设置为空白(但是在构造函数中它们已设置为空白以便考虑到这一点)。
有什么想法吗?
答案 0 :(得分:1)
也许您可以修改For Each
代码来处理DenialReason
的分配。这仍然感觉像是一个黑客,但我认为你拥有的东西可能更干净。如果您使用此代码,则不需要以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
变量假设一个基于1的索引。我认为这比使用IndexOf()
更清晰。
我认为更好的解决方案可能是拥有DenialReason
的列表并在项目为真时添加到该列表:
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
列表即可。这是灵活的,因此,如果由于某种原因,您有三个以上DenialReason
s,则不必添加另一个硬编码变量。