Solved

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

Posted on 2011-03-10
2
317 Views
Last Modified: 2012-06-21
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
Comment
Question by:tentavarious
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 35095278
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
 

Author Comment

by:tentavarious
ID: 35096076
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question