Using the anonymous object values inside the constructor when instantiating the class in VB.NET

2
Hello, I'm creating a solution to generate the Fiscal SPED files where I make all the validation rules of each class as soon as I build it. This is necessary because there are specific rules for each field, block, or registry. I was able to create my classes for types that already do all the validation but I'm having problems with Registers, like this:

Custom types according to the documentation

Namespace Tipos
    Public Class Caractere
        ...
    End Class
    Public Class Numerico(Of T)
        ...
    End Class
    Public Class Registro
        ...
    End Class
End Namespace

Representation of 1 record

Imports Sped.Tipos
Namespace Tipos.Bloco0
    Public Class Registro0000
        Inherits Registro

        ' Propriedades da classe
        Public COD_VER As Numerico(Of Integer)
        Public COD_FIN As Numerico(Of Integer)
        Public DT_INI As Caractere
        Public DT_FIN As Caractere
        Public NOME As Caractere
        Public CNPJ As Caractere
        Public CPF As Caractere
        Public UF As Caractere
        Public IE As Caractere
        Public COD_MUN As Numerico(Of Integer)
        Public IM As Caractere
        Public SUFRAMA As Caractere
        Public IND_PERFIL As Caractere
        Public IND_ATIV As Numerico(Of Integer)

        ' Construtor (aqui está o problema)
        Public Sub New()
            ... validação das regras do registro ...
        End Sub

    End Class
End Namespace

I need to pass the properties to the class and validate at the initialization of it, so I understand that it would use this looks like this:

Initialization of registration within block 0

Imports Sped.Tipos.Registros.Bloco0
Namespace Tipos.Blocos
    Public Class Bloco0

        Public registro0000 as Registro0000
        ... outros registros aqui

        Public Sub New()
            registro0000 = New Registro0000 With {
                .COD_VER = 1,
                .COD_FIN = 1, 
                .DT_INI = "20000101",
                .DT_FIN = "20200101"
                .NOME = "Foo"
                .CNPJ = "00000000000000"
                .CPF = ""
                .UF = "MG"
                .IE = ""
                .COD_MUN = 0000000
                .IM = ""
                .SUFRAMA = ""
                .IND_PERFIL = 1
                .IND_ATIV = 1
            }
            ... outras inicializações aqui
        End Sub

    End Class
End Namespace

In summary, I need to be able to use the parameters I'm passing using an initialization with an anonymous object inside the constructor of my registry to validate these values, but I'm not able to understand how to process this, and until then With {} only occurs after initialization ... How to solve?

    
asked by anonymous 27.04.2018 / 16:58

1 answer

0

Hi

Initialize the object in the constructor

Imports Sped.Tipos
Namespace Tipos.Bloco0
    Public Class Registro0000
        Inherits Registro

        ' Propriedades da classe
        Public Property COD_VER As Numerico(Of Integer)
        Public Property COD_FIN As Numerico(Of Integer)
        Public Property DT_INI As Caractere
        Public Property DT_FIN As Caractere
        Public Property NOME As Caractere
        Public Property CNPJ As Caractere
        Public Property CPF As Caractere
        Public Property UF As Caractere
        Public Property IE As Caractere
        Public Property COD_MUN As Numerico(Of Integer)
        Public Property IM As Caractere
        Public Property SUFRAMA As Caractere
        Public Property IND_PERFIL As Caractere
        Public Property IND_ATIV As Numerico(Of Integer)

        ' Construtor (aqui está o problema)
        Public Sub New( COD_VER As Numerico(Of Integer), COD_FIN As Numerico(Of Integer), DT_INI As Caractere, DT_FIN As Caractere, NOME As Caractere, CNPJ As Caractere, CPF As Caractere, UF As Caractere, IE As Caractere, COD_MUN As Numerico(Of Integer), IM As Caractere, SUFRAMA As Caractere, IND_PERFIL As Caractere, IND_ATIV As Numerico(Of Integer))

        Me.COD_VER = COD_VER 
        Me.COD_FIN = COD_FIN 
        Me.DT_INI = DT_INI 
        Me.DT_FIN = DT_FIN 
        Me.NOME = NOME 
        Me.CNPJ = CNPJ 
        Me.CPF = CPF 
        Me.UF = UF 
        Me.IE = IE 
        Me.COD_MUN = COD_MUN 
        Me.IM = IM 
        Me.SUFRAMA = SUFRAMA 
        Me.IND_PERFIL = IND_PERFIL 
        Me.IND_ATIV = IND_ATIV 


            ... validação das regras do registro ...
        End Sub

    End Class
End Namespace



Imports Sped.Tipos.Registros.Bloco0
Namespace Tipos.Blocos
    Public Class Bloco0

        Public registro0000 as Registro0000
        ... outros registros aqui

        Public Sub New()
            registro0000 = New Registro0000(, 1,, 1, , "20000101",, "20200101", "Foo", "00000000000000", "", "MG", "", 0000000, "", "", 1, 1
            )
            ... outras inicializações aqui
        End Sub

    End Class
End Namespace
    
13.07.2018 / 00:58