Hi
How to create a two dimensional array dynamically?
Thanks.
Printable View
Hi
How to create a two dimensional array dynamically?
Thanks.
You would have to pass a reference to the two dimensions, then use DIM to create the new array Dim MyArray(3,4) ' Creates 4x5 array starting with 0
or
Code:Dim MyX as Integer, MyY as Integer
MyX =3
MyY = 4
Dim MyArray(MyX,MyY)
dglienna,
actuall, I would like to do this:
Public Type ALLREC
row As Integer
col As Integer
table( , ) As String
.....
End Type
Private Sub myfunc(Rec As ALLREC)
Rec.row = 2
Rec.col = 4
Dim Rec.table(Rec.row, Rec.col) As String
....
End Sub
What's the correct way to do it?
Thank you!
Nope. You'd have to OVERLOAD the CONSTRUCTOR to set up the class with zero or more items.
Create a SUB inside of the class (and make it PUBLIC) so you can call it. Pass the parameters, then REDIM / Preserve or not the Array. Might need more for different array types
With the usage of the terms table row and col I am wondering why you are not using a recordset object?
Sorry if I mingle, but where is the problem? VB knows dynamic arrays. If I may make you recall the proper syntax:
You cannot use variables in a Dim statement. Only constants are allowed.Code:Dim MyArray() as String, x%, y%
...
'now we get the dimensions
x=4
y=5
ReDim MyArray(x, y)
Dim A() however makes a dynamic array, which gets its final dimensining with th ReDim statement, which allows the use of variables.
David: An interesting thesis. Which constructor of which class would you like to overload?
Btw.:
Dynamic Arrays can certainly be passed byref to a function/sub to be dimensioned there.
I think that should completely cover your problem, jwspring.Code:Dim arr$()
Private Sub DimArray(AnArray() As String)
Dim x%, y%
x = 3
y = 7 * 5
ReDim AnArray(x, y)
End Sub
Private Sub Command1_Click()
DimArray arr
End Sub
Oh, not to forget: ReDim Preserve allows to increase the array elements while keeping the existing content. Only I think you cannot change the number of dimensions anymore.
WoF - Zero Dimension, One Dimension, and Two Dimensions
Zero Dimension? Kinda like a simple variable? And what about 3D? ;)
He didn't say dimensions as in 3D I suppose. Also, you had this:
Code:Private Sub DimArray(AnArray() As String)
Yeah, I had this. What about it? :D
Anything wrong with it? :) Tested it within a small program. Works.
That would be how to pass a 0-Dimensional Array! :) only if you REDIM it correctly...
Yeah, right. :) We would also call it UNdimensioned. ;)