By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,515 Members | 1,042 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,515 IT Pros & Developers. It's quick & easy.

Dim Array() As New Class

P: n/a
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?

Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.
"Scott McNair" <sc**********@sfmco.takethispartout.com> schreef in bericht
news:Xn********************@207.46.248.16...
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?

Nov 21 '05 #2

P: n/a

"Scott McNair" <sc**********@sfmco.takethispartout.com> wrote in message
news:Xn********************@207.46.248.16...
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
Nov 21 '05 #3

P: n/a
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)

--------------------
Subject: Dim Array() As New Class
From: Scott McNair <sc**********@sfmco.takethispartout.com>
Message-ID: <Xn********************@207.46.248.16>
User-Agent: Xnews/5.04.25
Newsgroups: microsoft.public.dotnet.languages.vb
Date: Sun, 05 Jun 2005 20:24:25 -0700
NNTP-Posting-Host: 209-102-132-125.adsl.gulftel.net 209.102.132.125
Lines: 1
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP14.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.languages.vb:91136
X-Tomcat-NG: microsoft.public.dotnet.languages.vb

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?

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.