473,395 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Dim Array() As New Class

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
3 13246
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Joachim Dahl | last post by:
I would like to inherit the builtin array class and write som extension methods in C. Other people have suggested that I use numarray for that, but all I want is a simple continuous C array of...
2
by: zoe | last post by:
Is it possible to initialize a non-static const array class-member of a fundamental type? Something like: class Foo{}{ public: Foo(); private: const int myArray;
5
by: Mathieu Benoit | last post by:
(I'm having trouble to post this, sorry if this mail comes several times) I'm trying to optimize the use of an integer array class, and I found that one major problem is pointer aliasing. I...
9
by: Peter Olcott | last post by:
// // Array2D.h 2005-11-27 5:50 AM // #define UINT unsigned int // // // class ArrayType2D { private: int Width;
8
by: Protoman | last post by:
Is there any way I can improve this bounded array class? What other ops should I overload? How do I use operator() to make multiple subscripts? Here's the code: class OutOfBounds{}; template...
2
by: Big Charles | last post by:
Hello, I would like to create an array-class to be able to call like: Dim oMyCar as New MyCar ' After initializing oMyCar, the object has to be like: oMyCar(0).Brand...
9
by: Peri | last post by:
Dear All, I have written a common array class which has insert, search and other functions. For Example (Code in VB.NET): ' Client Class Public Class Client Public ClientCode As String ...
6
beacon
by: beacon | last post by:
Hi everybody, I asked my professor for some practice problems to work on over the summer to prep me for Data Structures this fall and he gave me Conway's Game of Life to recreate. I haven't had...
28
Nepomuk
by: Nepomuk | last post by:
Hi! I've read, that in C++ there's no predefined method, to calculate the size of an array. However, it's supposed to work withsizeof(array)/sizeof(array)Now, this does work in some situations, but...
0
emaghero
by: emaghero | last post by:
I am trying to use the attached header file, which provides me with a generic way of declarating arrays and matrices of arbitrary size. I would like to be able to use the classes Array and Matrix...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.