none
Instance a clas

    Question

  • I'm newbie in Visual Studio and my english is bad.

    I have class definition like this:

    Partial Public Class SuministroLRFacturasEmitidas Inherits SuministroInformacion

    Private registroLRFacturasEmitidasField() As LRfacturasEmitidasType

            <System.Xml.Serialization.XmlElementAttribute("RegistroLRFacturasEmitidas", Order:=0)>
     Public Property RegistroLRFacturasEmitidas() As LRfacturasEmitidasType()
      Get
       Return Me.registroLRFacturasEmitidasField
      End Get
     
      Set
       Me.registroLRFacturasEmitidasField = Value
       Me.RaisePropertyChanged("RegistroLRFacturasEmitidas")
      End Set 

     End Property
    End Class

    I can't instantiate the class, always return the same exception System.NullReferenceException

    Code is :

    Dim Registro() As ns_sii.LRfacturasEmitidasType

    For i = 0 To ....
           Registro(i) = New ns_sii.LRfacturasEmitidasType()
    next

    lSuministro.RegistroLRFacturasEmitidas = Registro

    Thanks

    Sunday, March 26, 2017 2:29 PM

Answers

  • Arrays cannot be declared with 'New'

    You should have either:

      Dim Registro As New ns_sii.LRfacturasEmitidasType

    if you want a single instance, or

      Dim Registro() As ns_sii.LRfacturasEmitidasType

    if you want an array.   If you want an array, then you have to dimension the array, and create a new instance for each array element. For instance:

      ReDim Registro (4)
      For I As Integer = 0 to 3
        Registro(I) = New ns_sii.LRfacturasEmitidasType
      Next I

    The same applies to registroLRFacturasEmitidasField. If you want that property to be a single instance use :
      Private registroLRFacturasEmitidasField As LRfacturasEmitidasType

    but if you want the property to be an array, use:
      Private registroLRFacturasEmitidasField() As LRfacturasEmitidasType

    and then dimension and initialise the array when the instance is created.

    • Edited by AcamarMVP Sunday, March 26, 2017 9:32 PM fmt
    • Marked as answer by josem67 Sunday, March 26, 2017 11:08 PM
    Sunday, March 26, 2017 9:24 PM

All replies

  • Hi

    Just a guess but try

    Dim Registro() As New ns_sii.LRfacturasEmitidasType


    Regards Les, Livingston, Scotland

    Sunday, March 26, 2017 2:36 PM
  • Instancing a class is the same as constructing a class. 

    Therefore has VB the keyword New

    Les gave you already the code. 


    Success
    Cor

    Sunday, March 26, 2017 3:42 PM
  • I tried this :

    Dim Registro() As New ns_sii.LRfacturasEmitidasType

    but i have this error:

    Arrays cannot be declared with 'New'

    Thanks

    Sunday, March 26, 2017 5:38 PM
  • Hi

    What is 'ns_sii.LRfacturasEmitidasType'?


    Regards Les, Livingston, Scotland

    Sunday, March 26, 2017 5:46 PM
  • Arrays cannot be declared with 'New'

    You should have either:

      Dim Registro As New ns_sii.LRfacturasEmitidasType

    if you want a single instance, or

      Dim Registro() As ns_sii.LRfacturasEmitidasType

    if you want an array.   If you want an array, then you have to dimension the array, and create a new instance for each array element. For instance:

      ReDim Registro (4)
      For I As Integer = 0 to 3
        Registro(I) = New ns_sii.LRfacturasEmitidasType
      Next I

    The same applies to registroLRFacturasEmitidasField. If you want that property to be a single instance use :
      Private registroLRFacturasEmitidasField As LRfacturasEmitidasType

    but if you want the property to be an array, use:
      Private registroLRFacturasEmitidasField() As LRfacturasEmitidasType

    and then dimension and initialise the array when the instance is created.

    • Edited by AcamarMVP Sunday, March 26, 2017 9:32 PM fmt
    • Marked as answer by josem67 Sunday, March 26, 2017 11:08 PM
    Sunday, March 26, 2017 9:24 PM
  • Perfect. It Works well.

    Thanks

    Sunday, March 26, 2017 11:07 PM