Solved

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

Posted on 2011-03-10
2
316 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

861 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now