在下面的代码中,我试图根据InspectionKey设置一个变量(bestInsp)等于集合中的最新变量(Inspections)。
我在for循环中使用了print语句,发现循环正常工作。但是,当我尝试在For循环之外使用bestInsp变量时,它不会保留我设置的值。根据我的研究,我认为这是一个块范围问题,并且解决方案说要在For-loop之外初始化变量(我已经完成)但它仍然无效。我有什么东西可以忽略吗?
Dim res As Result
Dim insp As Hansen.CDR.Building.IInspection
Dim bestInsp As Hansen.CDR.Building.IInspection
Dim oldKey As Integer = 0
Dim bestKey As Integer = 0
AddMaxFee = False
For Each insp In oBldgApp.Inspections
If insp.InspectionType.Code = "Foundation" Then
If insp.InspectionResult.Code.Length > 0 Then
oldKey = insp.InspectionKey
If oldKey >= bestKey Then
bestKey = oldKey
bestInsp = insp
End If
End If
End If
Next
If bestInsp.InspectionResult.Code.ToUpper = "FAILED" Then
AddMaxFee = True
End If
编辑:我通过删除
解决了这个问题Dim bestInsp As Hansen.CDR.Building.IInspection
并重新添加它。我想我的编译器很困惑?