ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   2D array of strings problem (https://www.excelbanter.com/excel-programming/444262-2d-array-strings-problem.html)

Robert Crandal[_2_]

2D array of strings problem
 
I have the following erroneous subroutine:

'=================================
Sub DoStuff (A as Integer, B as Integer)

Dim (A, B) as String ' This creates an error!

End Sub
'================================

Is it possible to create a 2D array whose size depends
on the values of A and B above? I'm basically
looking for a way to create a 2D array whose
dimensions are not known at runtime. The size
of this 2D array will stay constant for the duration
of the "DoStuff" procedure, so I only need to set
it's size once.

Any ideas?

- Robert



Walter Briscoe

2D array of strings problem
 
In message of Fri, 18
Feb 2011 23:18:12 in microsoft.public.excel.programming, Robert Crandal
writes
I have the following erroneous subroutine:

'=================================
Sub DoStuff (A as Integer, B as Integer)

Dim (A, B) as String ' This creates an error!


You have not named that array
Dim X(A, B) as String ' Gets no error


End Sub
'================================

Is it possible to create a 2D array whose size depends
on the values of A and B above? I'm basically
looking for a way to create a 2D array whose
dimensions are not known at runtime. The size
of this 2D array will stay constant for the duration
of the "DoStuff" procedure, so I only need to set
it's size once.

Any ideas?

- Robert



--
Walter Briscoe

joeu2004

2D array of strings problem
 
On Feb 18, 10:18*pm, "Robert Crandal" wrote:
Sub DoStuff (A as Integer, B as Integer)
Dim (A, B) as String * ' This creates an error!
End Sub

[....]
Is it possible to create a 2D array whose size depends
on the values of A and B above?


Yes: use ReDim instead Dim. For example:

Sub doit1(a As Long, b As Long)
ReDim s(a, b) As String
MsgBox "a=" & a & " b=" & b & Chr(10) & _
LBound(s, 1) & ":" & UBound(s, 1) & _
" " & LBound(s, 2) & ":" & UBound(s, 2)
End Sub

Sub callit1()
doit1 12, 34
doit1 23, 45
End Sub

However, in that form, the lower bound depends on the Option Base. It
would be prudent to specify it explicitly, for example:

ReDim s(1 to a, 1 to b) As String


The size of this 2D array will stay constant for the duration
of the "DoStuff" procedure, so I only need to set it's size once.


So place the ReDim statement early in the procedure.

However, if you mean that you want to set the size only on the first
call and retain the original size on subsequent calls even if a and b
are different, you could do the following:

Sub doit2(a As Long, b As Long)
Static first As Long
Static s 'must be Variant type
If first = 0 Then
'first call only
first = 1: ReDim s(a, b) As String
s(1, 1) = "s(1,1)": s(1, 2) = "s(1,2)"
s(2, 1) = "s(2,1)": s(2, 2) = "s(2,2)"
End If
MsgBox "a=" & a & " b=" & b & Chr(10) & _
LBound(s, 1) & ":" & UBound(s, 1) & _
" " & LBound(s, 2) & ":" & UBound(s, 2) & _
Chr(10) & _
":" & s(1, 1) & ":" & s(1, 2) & ":" & _
s(2, 1) & ":" & s(2, 2) & ":"
End Sub

Sub callit2()
doit2 12, 34
doit2 23, 45
End Sub

Robert Crandal[_2_]

2D array of strings problem
 
Thanks, that worked perfectly.

BTW, is it possible to have a 2D array dimensioned
like this:

ReDim (0 to A, 0 to 0) ' Is this possible?

I'm thinking that the second element will usually contain
ONE element, but it will be at index "0".



"joeu2004" wrote in message
...
On Feb 18, 10:18 pm, "Robert Crandal" wrote:
Sub DoStuff (A as Integer, B as Integer)
Dim (A, B) as String ' This creates an error!
End Sub
[....]
Is it possible to create a 2D array whose size depends
on the values of A and B above?

Yes: use ReDim instead Dim. For example:

Sub doit1(a As Long, b As Long)
ReDim s(a, b) As String
MsgBox "a=" & a & " b=" & b & Chr(10) & _
LBound(s, 1) & ":" & UBound(s, 1) & _
" " & LBound(s, 2) & ":" & UBound(s, 2)
End Sub



Robert Crandal[_2_]

2D array of strings problem
 
You can actually disregard my 2nd question, cuz apparently it
works, but I might not use that after all.

Thank you for your help!


"Robert Crandal" wrote in message
...
Thanks, that worked perfectly.

BTW, is it possible to have a 2D array dimensioned
like this:

ReDim (0 to A, 0 to 0) ' Is this possible?

I'm thinking that the second element will usually contain
ONE element, but it will be at index "0".



joeu2004

2D array of strings problem
 
On Feb 18, 11:09*pm, Walter Briscoe
wrote:
Robert Crandal writes
Sub DoStuff (A as Integer, B as Integer)
Dim (A, B) as String * ' This creates an error!


You have not named that array
Dim X(A, B) as String ' Gets no error


Until you try to call it. Then you get the runtime error "compile
error: constant expression required".


All times are GMT +1. The time now is 04:37 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com