This is giveing me a 'subscript out of range' error on the red line. What Is the correct way of doing it?Code:Dim MyReturn() As String
ReDim Preserve MyReturn(0, 1) As String
ReDim Preserve MyReturn((UBound(MyReturn(), 1) + 1), 1) As String
Printable View
This is giveing me a 'subscript out of range' error on the red line. What Is the correct way of doing it?Code:Dim MyReturn() As String
ReDim Preserve MyReturn(0, 1) As String
ReDim Preserve MyReturn((UBound(MyReturn(), 1) + 1), 1) As String
Because of the way Arrays are held in memory you can only change the last dimension (changing other dimensions involves a load of shuffling)
By any chance are you trying this?
ReDim Preserve MyReturn((UBound(MyReturn, 1) + 1), 1) As String
No Error here
ReDim Preserve MyReturn(0 To UBound(MyReturn()), 1)
Please elaborate. I would think that all it would really need to do is reserve enough space for 2 more elements at the end.Quote:
Originally Posted by Milk
That is the same thing. I just use MyReturn() to help remind myself that I'm working with an array.Quote:
Originally Posted by koolsid
Koolsid, it still won't work as you can't change anything but the last dimension a syntax that would work would be...Code:Dim MyReturn() As String
ReDim MyReturn(1, 0)
ReDim Preserve MyReturn(1, (UBound(MyReturn, 2) + 1))
Because you forgot the +1Quote:
Originally Posted by jmsrickland
ReDim Preserve MyReturn(0 To (UBound(MyReturn())+1), 1)
(edit: it will still give you an error, see next reply. PS: wow you guys are fast, thanks)
Koolsid, it still won't work as you can't change anything but the last dimension a syntax that would work would be...
Are you 100% sure?
Don't know if this will help but it works for me
ReDim MyArray(0 To k, 0 To m)
or
ReDim MyArray(0 To k, 1)
or
ReDim MyArray(k, 1)
I don't know if this is what you want but this didn't throw me any error
Dim MyReturn() As String
ReDim Preserve MyReturn(0, 1) As String
'MsgBox UBound(MyReturn, 1) + 1
ReDim MyReturn((UBound(MyReturn, 1) + 1), 1) As String
You can change any dimension if you are not preserving the existing data but each of those Redim statements use the same first dimension (0 to k), tryQuote:
Originally Posted by jmsrickland
it should get a subscript out of range errorCode:Dim X()
Redim X (1,1)
Redim preserve X (2,1)
It occurs to me that a 2-dimensional array is that the first parameter is the number of rows and the second parameter is the number of columns so you should always be able to change the first parameter wheather you preserve or not. I am not sure about the second parameter however or do I have it backwards..
perfectly backwards...
Hopefully this should show how the array is arranged in memory. You should be able to see how changing the last dimension involves little more than adding/losing chunks whereas anything else involves juggling.
Hope it makes some sense.Code:Option Explicit
Private Declare Sub RtlMoveMemory Lib "kernel32" (Destination As Any, Source As Any, ByVal Length As Long)
Private Sub Form_Load()
Dim ArrayX() As Byte, ArrayY() As Byte, i As Long
ReDim ArrayX(1, 1, 2)
ArrayX(0, 0, 0) = 1
ArrayX(1, 0, 0) = 2
ArrayX(0, 1, 0) = 3
ArrayX(1, 1, 0) = 4
ArrayX(0, 0, 1) = 5
ArrayX(1, 0, 1) = 6
ArrayX(0, 1, 1) = 7
ArrayX(1, 1, 1) = 8
ArrayX(0, 0, 2) = 9
ArrayX(1, 0, 2) = 10
ArrayX(0, 1, 2) = 11
ArrayX(1, 1, 2) = 12
ReDim ArrayY(11)
RtlMoveMemory ArrayY(0), ArrayX(0, 0, 0), 12 'copys the data sequentially from one array to the other
For i = 0 To UBound(ArrayY)
Debug.Print ArrayY(i);
Next i
End Sub
If what Milk posted doesn't make sense, just think of 2D as (X, Y). Knowing that X changes as you move left/right among columns, and Y changes as you move up/down among rows, it should be easy to remember that X = Columns and Y = Rows. Thus, 2D is (Columns, Rows) and not vice versa.Quote:
Originally Posted by jmsrickland
Here's why I thought it was row,column order. In the below code you see that I am redimensioning Beeps where I am changing the left parameter but never the right parameter. My thinking was that I was adding a new row of beep frequencies and durations each time the sub was entered.
So Im thinking this:
but, you say this is not the case so then how would that layout appear then?Code:ReDim Beeps(0 To UBound(s1), 0 To 1)
| | |
+--------------------------+ |
| | |
| +--------------------+
| | |
freq1, duration1 <--+
freq2, duration2 <--+
freq3, duration3 <--+
etc
etc
freqN, durationN <--+
Code:Private Sub cmdPlayBeeps_Click()
Dim Beeps() As Single
Dim s1() As String
Dim s2 As String
Dim s3() As String
Dim s4 As String
Dim s5 As String
Dim i As Integer
On Error Resume Next
s1 = Split(Text1.Text, vbCrLf)
ReDim Beeps(0 To UBound(s1), 0 To 1)
For i = 0 To UBound(s1)
s2 = s1(i)
If s2 = "" Then Exit For
s3 = Split(s2, ",")
s4 = s3(0)
s5 = s3(1)
Beeps(i, 0) = CSng(s4): Beeps(i, 1) = CSng(s5)
Next i
For i = 0 To UBound(Beeps)
Beep Beeps(i, 0), Beeps(i, 1)
Next i
End Sub
the order in memory would be...
If your using just Redim and not Redim Preserve your making a new array not adding to the old one.Code:ReDim Beeps(0 To N, 0 To 1)
Beeps(0, 0)
Beeps(1, 0)
Beeps(2, 0)
Beeps(3, 0)
...
Beeps(N, 0)
Beeps(0, 1)
Beeps(1, 1)
Beeps(2, 1)
Beeps(3, 1)
...
Beeps(N, 1)
So the graphical layout would be this instead of how I did it in my previous post.
What I find misleading is that, for example, a 1 dimensional array would only have the X parameter which runs horizonately but when compared to a Listbox for example which is a one dimensional array you see it as one entry on top of another entry which seems to contradict the order of how array are arrainged. It seems to me that the normal way that people would think is that an array is stacked in a vertical fasion rather than a horizontal fasionCode:Second Parameter
| +-- First Parameter------------>
| |
0 = f1, f2, f3, f4, f5, f6, f7.........fN
1 = d1, d2, d3, d4, d5, d6, d7.........dN
So in a Listbox you see this
Item 1
Item 2
Item 3
Item 4
but in reality it appears in memory like this
Item1 Item2 Item3 Item4.....