为什么我的列表在仅添加4个项目后计数为8?

时间:2017-09-14 10:57:43

标签: vb.net list csv io

我有一个包含以下内容的csv文件Questions.csv

Question ID, A, B, C, 
1, TestA, TestB, TestC
2, TestA, TestB, TestC
3, TestA, TestB, TestC

在我的代码中,我有以下方法从文件中读取并使用值创建一个Question对象,然后将Question对象添加到列表中。

Public Sub getQuestionsAndAnswers()

Dim allQuestions As New List(Of Question)

Using ioReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("Resources/Questions.csv")

    ioReader.TextFieldType = FileIO.FieldType.Delimited
    ioReader.SetDelimiters(",")
    'ioReader.ReadFields()
    While Not ioReader.EndOfData

        Dim arrCurrentRow As String() = ioReader.ReadFields()
        Dim aQuestion As Question = New Question(arrCurrentRow(0), arrCurrentRow(1), arrCurrentRow(2))
        allQuestions.Add(aQuestion)

    End While

End Using

当我使用allQuestions.count()检查列表的大小时,它等于8.为什么在应该只有4个时,列表中有8个项目?

1 个答案:

答案 0 :(得分:0)

虽然不理想,但这对我有用:

Public Sub getQuestionsAndAnswers()
    Dim allQuestions As New List(Of Question)

    Using ioReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("Resources/Questions.csv")

        ioReader.TextFieldType = FileIO.FieldType.Delimited
        ioReader.SetDelimiters(",")
        'ioReader.ReadFields()
        While Not ioReader.EndOfData

            Dim arrCurrentRow As String() = ioReader.ReadFields(0).Split(",")
            Dim aQuestion As Question = New Question(arrCurrentRow(0), arrCurrentRow(1), arrCurrentRow(2))
            allQuestions.Add(aQuestion)

        End While

    End Using
End Sub

在我的代码中检查并使用它返回4个结果,虽然我不得不进行额外的拆分,因为没有它,代码引发了异常,因为分隔符实际上没有工作......