3

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!

Michoko
  • 33
  • 2
  • Is it possible that the file doesn't use the same quote character as you are expecting? – Steven Doggart Nov 02 '18 at 13:05
  • Thanks Steven. I retyped the quotes in my 3-line file, just to be sure, but I get the same result. I also tried to specify the quote character like this: But same issue – Michoko Nov 02 '18 at 13:15
  • Also tried simple quotes in my CSV file, instead of double quotes, and specified the quote character like this: . Doesn't work either. – Michoko Nov 02 '18 at 13:18
  • I never used this library, but [the documentation](https://www.filehelpers.net/docs/html/T_FileHelpers_FileHelperEngine.htm) indicates that specific engines exist for different functionality. Perhaps you should be using the `DelimitedFileEngine` or `CsvEngine` instead or the generic `FileHelperEngine`. – TnTinMn Nov 02 '18 at 13:49
  • Thanks TnTinMn, the issues was fixed by replacing properties with simple fields, as explained in the answer. Cheers! – Michoko Nov 02 '18 at 18:10

1 Answers1

1

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.

shamp00
  • 11,106
  • 4
  • 38
  • 81