我正在尝试解析一个包含多个贷款的 .txt 文件。我的逻辑是:我用我需要的属性创建了一个 Loan 类,我创建了一个贷款对象列表。我创建了一个新的贷款对象并将其添加到列表中。然后我通读 txt 文件并填写对象属性。当我到达文件的末尾时,我需要创建一个新的贷款对象并重新开始,因为该文件中有多个贷款并且我需要每个贷款一个对象。问题是当我使用下面的代码时,我收到错误“局部变量'myLoans'隐藏了一个封闭块中的变量”。有一个更好的方法吗?
Public Class Loan
Public Property LoanId As String
Public Property LoanProvider As String
End Class
Dim listOfLoans As New List(Of Loan)()
Dim myLoans As New Loan
listOfLoans.Add(myLoans)
Dim line As String
Using r As New StreamReader("c:text.txt")
line = r.ReadLine()
Do While (Not line Is Nothing)
If (line.Substring(0, 10) = "Loan id:") Then
myLoans.LoanId = line.Substring(10, line.Length - 10).Trim()
ElseIf (line.Substring(0, 14) = "Loan Provider:") Then
myLoans.LoanProvider = line.Substring(14, line.Length - 19).Trim()
ElseIf (line.Substring(0, 30) = "Last line of file:") Then
Dim myLoans As New Loan
listOfLoans.Add(myLoans)
End If
line = r.ReadLine
Loop
End Using
提前致谢!