Solved

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

Posted on 2011-03-10
2
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

697 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