Parsing multiple fields with the file assistant

Good day to everyone

I have a problem with parsing CSV using File Helper. My CSV looks like this:

,, 026642,0,00336,05,19, "WATERMELON *", 19, "1" ,,,, 0, 001.99., 0 ,,, 0, 0, 0 ,,,,,,,, , 51 ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, 026645.0.00338.05.19, "ONION ", 19," * 1 ",,,, 0, 002.99., 0 ,,, 0, 0,0 ,,,,,,,,, 51 ,,,,,,,,,, ,,,,,,,,,,,,,,,,,,,,,,,,,, 026687,0.00380.05.19, "MUSHROOM", 19, "(BLACK FUNGUS)" ,, ,, 0, 021.90., 0 ,,, 0, 0, 0 ,,,,,,,,,, 51 ,,,,,,,,,,,,,,,,,,,, ,,,,,,,,,,,,,,

in which this CSV has 116 columns / fields.

The problem was that I just needed only 4 fields from the whole row, which

Field 4 = 026687, field 9 = "WATERMELON *", field 11 = "(BLACK FUNGUS)", field 21 = 002.99.

When I use a wizard, which is a very good solution to create an instance class, and I put FieldValueDiscarded _ on top of all the other fields that I don't need, it just prints the same as the CSV input.

Please give me some tips on how I can extract only the required field for writing to output.

thanks

UPDATED: after researching a few more, I finally realized this error. This error occurs because the class is not inherited, so I cannot get a specific field in another class. By declaring a display class as an inherited class, I can get a specific field.

However, I was fixated on how to extract functions inside a class inherited from the mapping class. This is my code `Import System.Text Import System.IO Import FileHelpers

Public class ProcessField: Inherits InputCSV

Public Function MyPLUc(ByVal value As Integer) Dim PLUc As String MyBase.PLU = value PLUc = (0 + 0 + value) Return (0 + 0 + value) End Function Public Function MyStatusc(ByVal value As Integer) MyBase.Status = value Dim Status As Integer If value > 0 Then Status = 800 Else Status = 900 End If Return (0 + Status) End Function Public Function MyUnitPricec(ByVal value As Integer) MyBase.UnitPrice = value Dim UP As Integer UP = value Dim bytes As Byte() = System.Text.Encoding.Unicode.GetBytes(UP) Return (0 + 0 + 0 + UP) End Function Public Function MyLabelFormat(ByVal value As Integer) Return (1 + 1) End Function Public Function MyEAN(ByVal value As Integer) Return (0 + 6) End Function Public Function MyCName(ByVal value As String) MyBase.CName1 = value Dim hexString As String = Hex(value) Return (hexString) End Function Public Function MyBCC(ByVal value As String) value = (0 + 0) Return (0 + 0) End Function 

End of class

and the problem is how I get all return value in this function

+1
source share
1 answer

I'm not sure I understand your question, but I think you are making things more complicated than you need.

You can simply create your own FileHelpers class with 116 string fields. They should be (public) fields, not properties. You should not subclass the FileHelpers class. Do not try to use the FileHelpers class as a regular class (i.e. With properties, functions, etc., Subclasses). Think of the FileHelpers class as the โ€œspecificationโ€ of your CSV format .

To import, you write something like:

 Dim engine As New FileHelperEngine(GetType(RecordSpec)) ' To Read Use: Dim results As RecordSpec() = DirectCast(engine.ReadFile("FileIn.txt"), RecordSpec()) 

Then you have an array of RecordSpec . Each RecordSpec will fill in all 116 fields, but just ignore fields that you don't need. Then swipe through the results and do whatever you want with the values. For example, perhaps you need to map the imported fields to another (more normal) MyProduct class with properties instead of fields, and possibly with additional logic.

 For Each recordSpec As RecordSpec In results Dim myProduct = New MyProduct() myProduct.Id = recordSpec.Field4 myProduct.Name = recordSpec.Field9 myProduct.Category = recordSpec.Field23 ' etc. Next 

In short: the RecordSpec class should only be used to determine the structure of a CSV file. You use it to populate a simple array with all the values โ€‹โ€‹from the file. Then you map the values โ€‹โ€‹to the more useful MyProduct class.

+2
source

Source: https://habr.com/ru/post/1443263/


All Articles