Array Question

  • Thread starter Thread starter Jim Heavey
  • Start date Start date
J

Jim Heavey

Hello, I was wonderin how you can tell the size of each dimmension of an
array. Say for instance you had and array declared as "Dim MyArray(4,2)
as String". How can you find the value for the first dimmension and/or
the second dimmension. It has a "Length" property, but I believe this
just gives you the size of the total array.

Thanks!!!!!!
 
Jim,

MyArray.GetLength(dimensionIndex)

or

UBound(MyArray, dimensionIndex) - 1



Mattias
 
Hi,
I assume you are programming in VB6. So if you have a declaration like

Dim myarray(4,5) as String

then you want to get the array dimesions which is 4 and 5. You can do this
in a round about way.Basically you would loop through the first dimesion of
the array until you get an exception. you can do this for both the
dimensions. Here is the code

Private Sub Command1_Click()
Dim x(4, 5) As Integer

On Error GoTo temp
Dim val As Integer

For i = 0 To 1000
val = i
y = x(0, i)
Next i

temp:
MsgBox val - 1

End Sub


Anand Balasubramanian
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks
 
Anand,
I assume you are programming in VB6.

Weird assumption, considering that this is a VB.NET group, and the
original message included a reference to the Length property, which
doesn't exist in VB6.

Private Sub Command1_Click()
Dim x(4, 5) As Integer

On Error GoTo temp
Dim val As Integer

For i = 0 To 1000
val = i
y = x(0, i)
Next i

temp:
MsgBox val - 1

End Sub

You've got to be kidding, right?



Mattias
 
Sorry about the assumption. I thought of VB6 as soon as I saw ubound in one
of the answers. But still you can use the same logic in VB.net. Here is the
code

Dim b(4, 5) As String
Dim i As Integer

Dim val As Integer
Dim temp As Integer


Try
For i = 0 To 1000
val = i
temp = b(0, i)
Next
Catch ex As System.IndexOutOfRangeException
MsgBox(val - 1)



End Try


Anand Balasubramanian
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks
 
And yes Matias is correct, the simplest way is to use the
GetLength(dimensionindex) value.




Anand Balasubramanian
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks
 
That is truely a goofy thing to have to do to come up with the lenght of
each dimmension. Java has an easy way to get at this information.
 
Jim,
That is truely a goofy thing to have to do to come up with the lenght of
each dimmension. Java has an easy way to get at this information.

Just to be perfectly clear, you *don't* have to do all that. I'm still
not sure why Anand came up with that code. The "right" way to do it is
to use UBound or Array.GetLength - just as easy as in Java.



Mattias
 
Back
Top