CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Hybrid View

  1. #1
    Join Date
    Jun 2006
    Posts
    40

    Question dynamically create two dimensional array

    Hi
    How to create a two dimensional array dynamically?
    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jun 2006
    Posts
    40

    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!

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    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.

  6. #6
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    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.

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: dynamically create two dimensional array

    WoF - Zero Dimension, One Dimension, and Two Dimensions
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    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?

  9. #9
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: dynamically create two dimensional array

    Zero Dimension? Kinda like a simple variable? And what about 3D?

  10. #10
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    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)
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  11. #11
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: dynamically create two dimensional array

    Yeah, I had this. What about it?
    Anything wrong with it? Tested it within a small program. Works.

  12. #12
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: dynamically create two dimensional array

    That would be how to pass a 0-Dimensional Array! only if you REDIM it correctly...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  13. #13
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    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
  •  





Click Here to Expand Forum to Full Width

Featured