Use the values of the anonymous object within the constructor when instantiating the class in VB.NET

Asked

Viewed 55 times

2

Hello, I am creating a solution to generate the Fiscal SPED files where I make all the validation rules of each class soon in the construction of the same. This is necessary because for each Field, Block or Record there are specific rules. I was able to create my classes for types that already do all the validation but I’m having problems with Records, like this:

Custom types according to 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 her initiation, so I understand I would use that being like this:

Startup of the record 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 short, I need to be able to use the parameters I am going through using a initialization with an anonymous object inside the constructor of my record to then validate these values but, I am not able to understand how to process this and until then the With {} only occurs after initialization... How to solve?

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.