0

I am having some problems to determine how can i remove the errors i am having using FileHelpers with the option strict on.

I initialized the class MasterDetails

Private _res As MasterDetails()

The function to call the MasterDetailEngine

Private Function ReadCsvFile(ByVal fileName As String, ByVal tipodoc As String) As Boolean
engine = New MasterDetailEngine(GetType(CabecDocVgr), GetType(LinhasDocVgr), CommonSelector.MasterIfContains,
                                            "@")
            _res = engine.ReadFile(strStartPath)
Return true
End Function

This is the class for the Master:

<DelimitedRecord(";")>
Public NotInheritable Class CabecDocVgr
    Public TipoLinha As String
    Public Doc As String
    Public Entidade As String
    Public DataDoc As String
    Public RefDoc As String
End Class

This one, for the Details:

<DelimitedRecord(";")> _
Public NotInheritable Class LinhasDocVgr
    Public Tipolinha As String
    Public Artigo As String
    Public Armazem As String
    Public Localizacao As String
    Public Lote As String
    Public Qtd As Integer
    Public UniMedida As String
    Public DataValidade As String
End Class

Each master/detail is to create a new document with a header and the related rows.

I can give one example where the error appears, when i call a member of the Master or Details class:

Private Function TransformaDocF(ByVal tipoDoc As String) As Boolean

    For numdoc As Integer = 0 To _res.Length - 1

        Dim documentosOrigem(0) As Object
        Dim docOrigem As New GcpBEDocumentoCompra
        Dim docDestino As New GcpBEDocumentoCompra

        Dim query As String = String.Format("SELECT TOP 1 NumDoc FROM cabecdoc WHERE RefDocOrig='{0}'",
                                            _res(numdoc).Master.doc)

        Dim lista As New StdBELista()   
        lista = Motor.Consulta(query)

        If Not lista.Vazia Then
            If Not lista.NoInicio And Not lista.NoFim Then
                Dim numeroDoc As Integer = CInt(lista.Valor("NumDoc"))
                'bExecuta = True
            End If
        End If

        docOrigem = Motor.Comercial.Compras.Edita("000", "VGR", "2013", _res(numdoc).Master.doc)
        documentosOrigem(0) = docOrigem

        docDestino.TipoEntidade = "F"
        docDestino.Serie = "2013"
        docDestino.Entidade = docOrigem.Entidade.TrimStart(CChar("0"))
        'DocDestino.DataDoc = "01-01-2013"
        docDestino.Tipodoc = "VFA"
        docDestino.RefDocOrig = _res(numdoc).Master.refDoc

        Try
            If docOrigem.Linhas.NumItens > 0 Then
                For numlinha As Integer = 0 To _res(numdoc).Details.Length - 1
                    If docOrigem.Linhas(numlinha + 1).Artigo = _res(numdoc).Details(numlinha).Artigo Then
                        docOrigem.Linhas(numlinha + 1).Quantidade = docOrigem.Linhas(numlinha + 1).QuantSatisfeita +
                                                                    _res(numdoc).Details(numlinha).Qtd
                    End If
                Next
            End If
            Motor.Comercial.Vendas.TransformaDocumentoEX2(documentosOrigem, CType(docDestino, GcpBEDocumentoVenda), True)
            Return True
        Catch ex As Exception

            MessageBox.Show(ex.Message)
            Return False
        End Try

    Next
    Return True
End Function

For example, calling _res(numdoc).Master.refDoc gives me the error: " Option Strict On disallows late binding"

Any ideas about what i need to modify so the error can be gone?

Thank you

Filipe Costa
  • 655
  • 3
  • 13
  • 30

1 Answers1

1

Try using generics. I'm not very good at VB.NET but I think it should look something like this:

'declare _res as an array of MasterDetails<CabecDocVgr, LinhasDocVgr>
Dim _res As MasterDetails(Of CabecDocVgr, LinhasDocVgr)()

'instantiate the generic version of the FileHelpers engine
Dim engine = New MasterDetailEngine(Of CabecDocVgr, LinhasDocVgr)(CommonSelector.MasterIfContains, "@")

'should not cause 'late binding error' since the results are strongly typed
_res = engine.ReadFile("")
shamp00
  • 11,106
  • 4
  • 38
  • 81
  • And that is what i needed! :) No more errors. Just one more thing. I have more classes, do i need to repeat all that code for each pair of CabecX, LinhasDocX or i can simplify the code to use only the class i need in some moment? – Filipe Costa Dec 18 '13 at 17:46
  • 1
    You could avoid duplicating code by creating a factory method to create the correct FileHelpers engine: something like `EngineFactory.Create(masterType as Type, detailType as Type)` which will return a new `MasterDetailEngine(Of CabecDocVgr, LinhasDocVgr)`. – shamp00 Dec 19 '13 at 09:58
  • 1
    Using generics (as above) will mean that `_res.Master` contains an array of `CabecDocVgr`. With the non-generic version it will contain an array of `object` instead. The 'late binding' error occurs when you try to do `object`.`refDoc`. (So you could still use your original implementation if you were always careful to cast `Master` to the correct type before using `refDoc`. But it's cleaner to use generics.) – shamp00 Dec 19 '13 at 09:58