creating a two dimensional array

R

rguti

Hi,

How do I create a two dimensional array?

I have created a one dimensional doing this:

Dim laFields As ArrayList = New ArrayList

How about to do a 2 dimensional?

Thanks.
 
S

SStory

What you have created is an arraylist.

I don't think arraylists do two dimensions inherently.
It think the idea is to add objects to the array --which is just a glorified
collection.

Your objects then have all of the properties that you need in themselves so
you don't need multiple dimensions.

If however you want a multidimensional,actual array in VB

dim x(2,3,4) as integer

will give you a 3 dimensional array of integers.

depends on what you want to accomplish.

HTH,

Shane
 
H

Herfried K. Wagner [MVP]

* rguti said:
How do I create a two dimensional array?

I have created a one dimensional doing this:

Dim laFields As ArrayList = New ArrayList

That's not an array, that's an arraylist.
How about to do a 2 dimensional?

\\\
Dim aint(99, 99) As Integer
///
 
T

The Grim Reaper

Dim vArray()() As Integer
ReDim vArray(10)(20)
vArray(10)(10) = 10 ' etc..
___________________________________
The Grim Reaper
 
J

Jeff Johnson [MVP: VB]

Dim vArray()() As Integer
ReDim vArray(10)(20)
vArray(10)(10) = 10 ' etc..

Assuming that's even allowed syntax, you've got an array of arrays, not a
2-dimensional array.
 
J

Jay B. Harlow [MVP - Outlook]

Grim,
As Jeff suggests, you created an array of arrays (also referred to as a
Ragged or Ragged-Row Array), which is slightly different then a 2
dimensional array.

To create a 2 dimensional array 10 x 20 (11 x 21 really) you would need to
use Herfrieds or SStory's syntax of:

Dim vArray(,) As Integer
Redim vArray(10, 20)

An array of array is called a ragged array because each row does not need to
be the same length! (The ends of the rows are ragged, they don't line up).

Dim vArray()() As Integer
' size the outer array
Redim vArray(10)

For index As Integer = 0 to 10
' size each of the inner arrays
Redim vArray(index)(index)
Next

Where we have an array of arrays, where each row is 1 column longer then the
previous row.

The following article does a good job of describing the various kinds of
arrays in .NET.

Hope this helps
Jay
 
C

Cor Ligthert

Jay,
The following article does a good job of describing the various kinds of
arrays in .NET.
I did not see the link to the article in in the message.

I probably will not use it however I attent you on it because if someone
starts to Google this thread

Cor
 
R

Ricky W. Hunt

The Grim Reaper said:
Dim vArray()() As Integer
ReDim vArray(10)(20)

I'm new to VB and I don't understand this ReDim. Why not just do a:

Dim vArray(10, 20) As Integer

and be done with it?
 
C

Cor Ligthert

Hi Ricky,

A redim is a redimension of your array, as soon as you have to use it, than
it is better to look to a Net collection (not the VB one) to use or to make
your own class for it or to make an arraylist of your own class objects.

I hope this helps?

Cor
 
H

Herfried K. Wagner [MVP]

* Ricky W. Hunt said:
I'm new to VB and I don't understand this ReDim. Why not just do a:

Dim vArray(10, 20) As Integer

and be done with it?

This would create a 2-dimensional array, not a jagged array.

For information to 'ReDim', place the caret on it and press F1.
 
T

The Grim Reaper

Because you can't define a jagged array using Dim vArray(10, 10), as
Herfried said.
I also agree with Cor - I very rarely use an array of more than one
dimension, it makes much more sense in most situations to use collections,
arraylist, hashtables or customised collection classes.
_________________________________
The Grim Reaper
 
T

The Grim Reaper

Like I said..... I don't usually use arrays.
I simply tried to create a mulidimensional array in the IDE and that's the
syntax that worked.
Sorry.
_________________________________
The Grim Reaper
 
J

Jay B. Harlow [MVP - Outlook]

Ricky,
In addition to Herfried's & Grim's comments. See my other posts in this
thread:
is a jagged or ragged or ragged-row array, while
Dim vArray(10, 20) As Integer
is a two-dimensional array.

Hope this helps
Jay
 
R

Ricky W. Hunt

Cor Ligthert said:
Hi Ricky,

A redim is a redimension of your array, as soon as you have to use it, than
it is better to look to a Net collection (not the VB one) to use or to make
your own class for it or to make an arraylist of your own class objects.

I hope this helps?

OK. So this is only for "reusing" the name/memory and resizing it? If it's a
static array that never changes size there's no need to Redim? Thanks.
 
J

Jay B. Harlow [MVP - Outlook]

Ricky,
You are correct.

However! ReDim is handy in the constructor of a class also, where you do not
know the size of the array when you define the class, you only know the size
when you create an instance of the class.

Something like:

Public Class MyBuffer

Private m_buffer() As Byte

Public Sub New(count As Integer)
ReDim m_buffer(count - 1)
End Sub

End Class

Dim b1 As New MyBuffer(10)
Dim b2 As New MyBuffer(20)

Hope this helps
Jay
 
R

Ricky W. Hunt

Jay B. Harlow said:
Ricky,
You are correct.

However! ReDim is handy in the constructor of a class also, where you do not
know the size of the array when you define the class, you only know the size
when you create an instance of the class.

Thanks.
 

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