Dim Array() As New Class

S

Scott McNair

Hi,

I'm trying to populate a 2-dimensional array by assigning each item its own
instance of a class, for example:

Dim SubSector(8,10) As New SystemClass

....where SystemClass is a class I've defined elsewhere in my code.

However, it won't let me use "New" in the declaration (it gives the error
"Arrays cannot be declared with 'New'") and if I leave "New" out, then each
section of the array contains the exact same data - it's as though instead
of having its own instance of SystemClass, each section is referring to the
original class.

How would I go about making each instance unique unto itself, rather than
all clones of each other?
 
P

Peter Proost

Ho Scott,

maybe this bit of code can help you,

put a button on a form and copy paste the first bit of this code in it's
click event and the next bit behind your form's end class:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles _ Button1.Click
Dim MyInfoArr(2) As Myinfo
For i As Integer = 0 To 2
MyInfoArr(i) = New Myinfo
Select Case i
Case 0
MyInfoArr(i).MyName = "Peter"
MyInfoArr(i).MyCountry = "Belgium"
Case 1
MyInfoArr(i).MyName = "Piedro"
MyInfoArr(i).MyCountry = "Jhonnytown"
Case 2
MyInfoArr(i).MyName = "Mr. Whiskers"
MyInfoArr(i).MyCountry = "Whiskycity"
End Select
Next
For i As Integer = 0 To 2
MsgBox(MyInfoArr(i).ToString)
Next
End Sub

Public Class Myinfo
Private strMyName As String
Private strMyCountry As String
Public Property MyName() As String
Get
Return strMyName
End Get
Set(ByVal Value As String)
strMyName = Value
End Set
End Property
Public Property MyCountry() As String
Get
Return strMyCountry
End Get
Set(ByVal Value As String)
strMyCountry = Value
End Set
End Property

Public Overrides Function ToString() As String
Return "Hi my name is: " & strMyName & " and I live in: " &
strMyCountry & "."
End Function

End Class

Hth Greetz Peter
 
D

David Lee Conley

Scott McNair said:
Hi,

I'm trying to populate a 2-dimensional array by assigning each item its
own
instance of a class, for example:

Dim SubSector(8,10) As New SystemClass

...where SystemClass is a class I've defined elsewhere in my code.

However, it won't let me use "New" in the declaration (it gives the error
"Arrays cannot be declared with 'New'") and if I leave "New" out, then
each
section of the array contains the exact same data - it's as though instead
of having its own instance of SystemClass, each section is referring to
the
original class.

How would I go about making each instance unique unto itself, rather than
all clones of each other?

I'm still relatively new to VB.NET so I'm not sure this will work, but have
you tried:

Dim SubSector(8,10) as SystemClass
Dim i as Integer, j as Integer

for i = 0 to 8
for j = 0 to 10
SubSector(i,j) = New SystemClass
next j
next i

Dave
 
A

Alexandre Moura

Think of it this way:
Dim SubSector(8,10) As New SystemClass
is similar to:
Dim SubSector(8,10) As SystemClass = New SystemClass

You either need to use the array approach as per David's email, or the following:
Dim SubSector(,) As SystemClass = New SystemClass(,) {{New SystemClass, New SystemClass, New SystemClass, New SystemClass}, _
{New SystemClass, New SystemClass, New SystemClass, New SystemClass}}

(initializes a 2x3 array - in this case, it's probably easier to just use the nested for approach)

Hope that helps,
Alex (MS VB QA)

--------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top