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

creating a two dimensional array

P: n/a
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.
Nov 20 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
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
"rguti" <te**@test.com> wrote in message
news:OB**************@TK2MSFTNGP12.phx.gbl...
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.

Nov 20 '05 #2

P: n/a
Dim vArray()() As Integer
ReDim vArray(10)(20)
vArray(10)(10) = 10 ' etc..
___________________________________
The Grim Reaper

"rguti" <te**@test.com> wrote in message
news:OB**************@TK2MSFTNGP12.phx.gbl...
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.

Nov 20 '05 #3

P: n/a
* rguti <te**@test.com> scripsit:
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
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #4

P: n/a

"The Grim Reaper" <gr*********@btopenworld.com> wrote in message
news:cd**********@titan.btinternet.com...
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.
Nov 20 '05 #5

P: n/a
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
"The Grim Reaper" <gr*********@btopenworld.com> wrote in message
news:cd**********@titan.btinternet.com...
Dim vArray()() As Integer
ReDim vArray(10)(20)
vArray(10)(10) = 10 ' etc..
___________________________________
The Grim Reaper

"rguti" <te**@test.com> wrote in message
news:OB**************@TK2MSFTNGP12.phx.gbl...
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.


Nov 20 '05 #6

P: n/a
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
Nov 20 '05 #7

P: n/a
"The Grim Reaper" <gr*********@btopenworld.com> wrote in message
news:cd**********@titan.btinternet.com...
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?
Nov 20 '05 #8

P: n/a
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
Nov 20 '05 #9

P: n/a
* Ricky W. Hunt <rh*****@hotmail.com> scripsit:
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?


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

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

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #10

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

"Ricky W. Hunt" <rh*****@hotmail.com> wrote in message
news:xi5Kc.111185$XM6.59523@attbi_s53...
"The Grim Reaper" <gr*********@btopenworld.com> wrote in message
news:cd**********@titan.btinternet.com...
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?

Nov 20 '05 #11

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

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OS**************@TK2MSFTNGP09.phx.gbl...
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
"The Grim Reaper" <gr*********@btopenworld.com> wrote in message
news:cd**********@titan.btinternet.com...
Dim vArray()() As Integer
ReDim vArray(10)(20)
vArray(10)(10) = 10 ' etc..
___________________________________
The Grim Reaper

"rguti" <te**@test.com> wrote in message
news:OB**************@TK2MSFTNGP12.phx.gbl...
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.



Nov 20 '05 #12

P: n/a
Doh!
Here is the link to the article:

http://msdn.microsoft.com/library/de...et6_update.asp

Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:OS**************@TK2MSFTNGP09.phx.gbl...
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
"The Grim Reaper" <gr*********@btopenworld.com> wrote in message
news:cd**********@titan.btinternet.com...
Dim vArray()() As Integer
ReDim vArray(10)(20)
vArray(10)(10) = 10 ' etc..
___________________________________
The Grim Reaper

"rguti" <te**@test.com> wrote in message
news:OB**************@TK2MSFTNGP12.phx.gbl...
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.



Nov 20 '05 #13

P: n/a
Ricky,
In addition to Herfried's & Grim's comments. See my other posts in this
thread:
Dim vArray()() As Integer
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

"Ricky W. Hunt" <rh*****@hotmail.com> wrote in message
news:xi5Kc.111185$XM6.59523@attbi_s53... "The Grim Reaper" <gr*********@btopenworld.com> wrote in message
news:cd**********@titan.btinternet.com...
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?

Nov 20 '05 #14

P: n/a
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:O$*************@TK2MSFTNGP10.phx.gbl...
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.
Nov 20 '05 #15

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

"Ricky W. Hunt" <rh*****@hotmail.com> wrote in message
news:OwcKc.114218$Oq2.90853@attbi_s52...
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:O$*************@TK2MSFTNGP10.phx.gbl...
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.

Nov 20 '05 #16

P: n/a
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
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.
Nov 20 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.