-
September 7th, 2011, 10:45 AM
#1
dynamically create two dimensional array
Hi
How to create a two dimensional array dynamically?
Thanks.
-
September 7th, 2011, 12:32 PM
#2
Re: dynamically create two dimensional array
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)
-
September 7th, 2011, 12:51 PM
#3
Re: dynamically create two dimensional array
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!
-
September 7th, 2011, 10:31 PM
#4
Re: dynamically create two dimensional array
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
-
September 7th, 2011, 11:14 PM
#5
Re: dynamically create two dimensional array
With the usage of the terms table row and col I am wondering why you are not using a recordset object?
Always use [code][/code] tags when posting code.
-
September 8th, 2011, 04:28 AM
#6
Re: dynamically create two dimensional array
Btw.:
Dynamic Arrays can certainly be passed byref to a function/sub to be dimensioned there.
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
I think that should completely cover your problem, jwspring.
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.
-
September 8th, 2011, 12:34 PM
#7
Re: dynamically create two dimensional array
WoF - Zero Dimension, One Dimension, and Two Dimensions
-
September 8th, 2011, 04:22 AM
#8
Re: dynamically create two dimensional array
Sorry if I mingle, but where is the problem? VB knows dynamic arrays. If I may make you recall the proper syntax:
Code:
Dim MyArray() as String, x%, y%
...
'now we get the dimensions
x=4
y=5
ReDim MyArray(x, y)
You cannot use variables in a Dim statement. Only constants are allowed.
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?
-
September 8th, 2011, 12:58 PM
#9
Re: dynamically create two dimensional array
Zero Dimension? Kinda like a simple variable? And what about 3D?
-
September 8th, 2011, 03:27 PM
#10
Re: dynamically create two dimensional array
He didn't say dimensions as in 3D I suppose. Also, you had this:
Code:
Private Sub DimArray(AnArray() As String)
-
September 9th, 2011, 04:30 PM
#11
Re: dynamically create two dimensional array
Yeah, I had this. What about it?
Anything wrong with it? Tested it within a small program. Works.
-
September 9th, 2011, 06:07 PM
#12
Re: dynamically create two dimensional array
That would be how to pass a 0-Dimensional Array! only if you REDIM it correctly...
-
September 13th, 2011, 09:56 AM
#13
Re: dynamically create two dimensional array
Yeah, right. We would also call it UNdimensioned.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|