Note that there are some explanatory texts on larger screens.

plurals
  1. POGeneric function to convert a CSV file to an object
    primarykey
    data
    text
    <p>I want to implement generic method to read two types of CSV files. I have created two classes to represent each of those csv files. Can any one tell me how to write a generic function ReadFile(Of T) and generate objects.</p> <p>Thanks</p> <p>I'm adding the function I have created for this. Please advise the correct way of achieving this.</p> <pre><code> Public Function ReadFile(Of THeader, TRecord, TTrailer, TResult)(fileName As String) As IData(Of TRecord) Dim objHeader As IHeader = Nothing Dim objTrailer As ITrailer = Nothing Dim objRecord As TRecord = Nothing Dim colRecords As Collection(Of TRecord) = Nothing Dim objData As IData(Of TRecord) = Nothing Using objDataReader As OleDbDataReader = objTextReader.Read("SELECT * FROM [" &amp; fileName &amp; "] WHERE RecordType='A'") If objDataReader.HasRows Then While objDataReader.Read objHeader = Activator.CreateInstance(Of THeader)() With objHeader.GetType .GetProperty("ReportType").SetValue(objHeader, objDataReader(1).ToString, Nothing) .GetProperty("FileDate").SetValue(objHeader, objDataReader(2).ToString, Nothing) .GetProperty("FileTime").SetValue(objHeader, objDataReader(3).ToString, Nothing) End With End While End If End Using Using objDataReader As OleDbDataReader = objTextReader.Read("SELECT * FROM [" &amp; fileName &amp; "] WHERE RecordType='B'") colRecords = New Collection(Of TRecord) Dim objPropInfo() As PropertyInfo If objDataReader.HasRows Then While objDataReader.Read objRecord = Activator.CreateInstance(Of TRecord)() objPropInfo = objRecord.GetType.GetProperties() For Each propInfo In objPropInfo If propInfo.Name &lt;&gt; "FormattedAccountTitle" And propInfo.Name &lt;&gt; "FormattedParticipantName" Then 'propInfo.SetValue(objRecord, If(objDataReader(propInfo.Name) Is DBNull.Value, "", objDataReader(propInfo.Name)), Nothing) propInfo.SetValue(objRecord, objDataReader(propInfo.Name).ToString, Nothing) End If Next colRecords.Add(objRecord) End While End If End Using Using objDataReader As OleDbDataReader = objTextReader.Read("SELECT * FROM [" &amp; fileName &amp; "] WHERE RecordType='Z'") If objDataReader.HasRows Then While objDataReader.Read objTrailer = Activator.CreateInstance(Of TTrailer)() With objTrailer.GetType .GetProperty("RecordCount").SetValue(objTrailer, objDataReader(1).ToString, Nothing) End With End While End If End Using objData = Activator.CreateInstance(Of TResult)() With objData.GetType .GetProperty("Header").SetValue(objData, objHeader, Nothing) .GetProperty("Records").SetValue(objData, colRecords, Nothing) .GetProperty("Trailer").SetValue(objData, objTrailer, Nothing) End With Return objData End Function </code></pre> <p><strong>Below is the sample CSV files:</strong></p> <p><strong>CSV1:</strong></p> <pre> A,T18,04SEP2013,101540,,,,,,,,,,,,,,,,,,, B,012345678,2013,,,,,,,,,,,,,,,,,,,, B,,,1234567890,Mr Johnathan Peter Jones,Mr,Johnathan Peter,Jones,,Y,2,1,2,"Griffin House, Silver Head Street",Sheffield,,,,12A456,ZZ,01041960,10245.45,2548.15 B,,,1234567890,Miss Jane Elizabeth Smith,Mr,Johnathan Peter,Jones,,Y,2,2,2,"Griffin House, Silver Head Street",Sheffield,,,,12A456,ZZ,01041960,10245.45,2548.15 B,,,1234567899,Mr JSimon Soanes,Mr,Simon,Soanes,,Y,1,1,1,Abc Building Xyz street,Sheffield,,,,12AB516,ZZ,01091966,88245.45,4148.15 Z,3,,,,,,,,,,,,,,,,,,,,, </pre> <p><strong>CSV2:</strong></p> <pre> A,T17,17SEP2013,143021,,,,,,,,,,,,,,,,,,,,,,, B,1SDFDDGT,,2013,,,,,,,,,,,,,,,,,,,,,,, B,,,400109,1234FFFFFFFFFFF,Samsung Mobiles,,,,Samsung,N,1,1,1,Samsung Corporation Private Limited,"West Avenue, 4th Division, Devonshire Lane",,,United Kingdom,258DFF,ZZ,21524154,,123888.25,2562.78,Y,N B,,,400120,1234FSDUFFFFFFF,Sony Mobiles,,,,Sony,N,1,1,1,Sony Corporation Private Limited,,,,United Kingdom,255DFF,ZZ,216666154,,123888.25,2562.78,N,N Z,2,,,,,,,,,,,,,,,,,,,,,,,,, </pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload