Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

17 Experts available now in Live!

Get 1:1 Help Now