Search code examples
vb.netfilehelpers

FieldQuoted doesn't seem to work properly


I'm using FileHelpers 3.3 with VB .net. I tried to find an answer for a while, but all info I found referred to a behavior I'm not experiencing. So I begin to suspect a bug in FileHelpers.

I have a very simple CSV:

Description,Transaction Date,Transaction Time
01567,"Sep 1, 2018",12:47:32 AM PDT
01567,"Sep 1, 2018",12:47:32 AM PDT
81475,"Sep 1, 2018",12:52:18 AM PDT

So I declared a class in VB to contain those data:

<DelimitedRecord(","), IgnoreFirst(1)>
Public Class Sale
    Public Property description() As String
    <FieldQuoted(QuoteMode.AlwaysQuoted)>
    Public Property transactionDate() As String
    Public Property transactionTime() As String
End Class

When I read the file with:

Dim engine As FileHelperEngine(Of Sale) = New FileHelperEngine(Of Sale)
Dim records = engine.ReadFile("D:\Projects\test.txt")

I get the following error: FileHelpers.BadUsageException : 'Line: 2 Column: 32. Delimiter ',' found after the last field '_transactionTime' (the file is wrong or you need to add a field to the record class)'

This is caused by the comma inside the second field. If I remove the comma, all works fine. But even if I remove the comma, the transactionDate string is still surrounded by quotes.

From what I read in forums, FieldQuoted should:

  • handle the comma in the quoted field properly
  • remove the quotes in the processed string

But it does not work. It looks like the FieldQuoted instruction is simply ignored. I could try to handle those cases with a BeforeReadRecord event, but I thought FieldQuoted was created exactly to handle this use case. Can you please tell me if I missed something obvious?

Thank you very much!


Solution

  • FileHelpers requires fields not properties in VB. The following works as expected.

    <DelimitedRecord(","), IgnoreFirst(1)>
    Public Class Sale
        Public description As String
        <FieldQuoted(QuoteMode.AlwaysQuoted)>
        Public transactionDate As String
        Public transactionTime As String
    End Class
    

    Originally FileHelpers only supported fields. Support for C# auto-implemented properties was added in version 3, but there is no support yet for properties in VB.NET. There is a long-outstanding Github issue here.