Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 327
  • Last Modified:

how to create a array from a new instance of a class

Experts, i am trying to create an array object from a class, but i get an error arrays cannot be declared as new.  When i try to run i get instance not set to an instance of an object.  Any ideas?  Can someone provide me with an example?

In my form code
dim ipa as new ipdata  

ipa.ID_HEAD(0).Supplier_IP = "13.15.23.18"   'not set to instance of an object

I shorten up the class a lot, but there are a lot more variables.
Partial Public Class IPDATA
    Private ip_HEADERField() As Header_Info    
   Public Property ID_HEAD() As Header_Info()
        Get
            Return Me.ip_HEADERField       
      End Get
        Set
            Me.ip_HEADERField= value
        End Set
    End Property
End Class

Partial Public Class Header_Info    
    Private IP_ReferenceField As String
        
       Public Property Supplier_IP() As String
        Get
            Return Me.IP_ReferenceField         
       End Get
        Set
            Me.IP_ReferenceField  = value
        End Set
    End Property
    
   End Class

Open in new window

0
tentavarious
Asked:
tentavarious
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You need to give the array a size and then also instantiate each slot so it has a valid instance in it.

For example:  
Partial Public Class IPDATA

    Private ip_HEADERField(10) As Header_Info

    Public Sub New()
        For i As Integer = 0 To ip_HEADERField.GetUpperBound(0)
            ip_HEADERField(i) = New Header_Info
        Next
    End Sub

    Public Property ID_HEAD() As Header_Info()
        Get
            Return Me.ip_HEADERField
        End Get
        Set(ByVal value As Header_Info())
            Me.ip_HEADERField = value
        End Set
    End Property

End Class

Open in new window


Also, I would use List(Of Header_Info) instead of an Array.
0
 
tentavariousAuthor Commented:
perfect
I was going to remove the arrays out of the main class and try something like this, but i think your ways is better

  Dim ipArray(10) As IPDATA
       For i = 0 To 9
        Dim myip As IPDATA= New IPDATA()
        myip.ip_head="test"
          ipArray(i) = myip        '
       Next
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now