3 Replies - 8703 Views - Last Post: 31 July 2007 - 12:15 PM Rate Topic: -----

#1 travis.lee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-July 07

How can I combine 1D arrays into a 2D array? (VB6)

Post icon  Posted 31 July 2007 - 10:01 AM

I am trying to create a subroutine (in Visual Basic 6) that will combine one-dimensional arrays into a two-dimensional public array.

Desired characteristics of this subroutine:

1) One-dimensional arrays are passed to this subroutine

2) The subroutine generates a public two-dimensional array that is a combination of the one-dimensional arrays passed to the subroutine

3) Upon passing the first one-dimensional array into this subroutine, the public array generated by the subroutine is equal to this first one-dimensional array.

4) Each subsequent 1D array passed to the subroutine becomes a new row in the two-dimensional public array (where the first row is the first 1D array passed, the second row is the second 2D array passed, etc.)

5) The 2D public array generated by the subroutine should be able to be used by other functions or subroutines and then be able to be given another row (from another 1D array passed to the subroutine) and used again.

Any suggestions as to how to do this would be greatly appreciated. Elegance (in the code) is desired, but not at the expense of readability

Is This A Good Question/Topic? 0
  • +

Replies To: How can I combine 1D arrays into a 2D array? (VB6)

#2 tody4me  Icon User is offline

  • Banned
  • member icon

Reputation: 12
  • View blog
  • Posts: 1,398
  • Joined: 12-April 06

Re: How can I combine 1D arrays into a 2D array? (VB6)

Posted 31 July 2007 - 10:15 AM

define a global array
redim it when you pass another 1d array to it
when done, the global array should be the 2d array that combines all previous 1d arrays.

For further help, or any code suggestions, please provide what you have done so far, and not as a hw assignment.
Was This Post Helpful? 0
  • +
  • -

#3 travis.lee  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-July 07

Re: How can I combine 1D arrays into a 2D array? (VB6)

Posted 31 July 2007 - 11:42 AM

This is most certainly not a homework project, and I posted such a description because I am not exactly sure how to get started.

I am unable to redim a global array (I continually get the error "array has already been dimensioned"). "redim" appears only to work for objects declared with the "dim" feature rather than "global," "public," or "private." Also, using "redim" erases any previous entries in the array being dimensioned, so it will not work. I have attempted to make use of the "Preserve" feature, but I have run into trouble attempting to define an array with a variable row length (I receive an error saying that the row length must be a constant). The following is some code that I've been playing with. Take a look at it if you want, but it is fundamentally flawed in the sense that it relies on the notion of a variable row length.

Private Sub Form_Load()
counter = 0
End Sub

Private Sub Command1_Click()

qvolt 1, 2, 3
qvolt 4, 5, 6
qvolt 7, 8, 9

End Sub
Private Sub qvolt(ByVal timevalue As Integer, ByVal linevalue As Integer, output As Integer)

Dim dummy(counter, 2) As Integer

dummy(counter, 0) = timevalue
dummy(counter, 1) = linevalue
dummy(counter, 2) = output

Public matrixout(counter, 2) As Integer
matrixout(counter, 2) = dummy(counter, 2)

counter = counter + 1
ReDim Preserve dummy(counter, 2) As Integer

Debug.Print matrixout(0, 0), matrixout(0, 1), matrixout(0, 2)
Debug.Print matrixout(1, 0), matrixout(1, 1), matrixout(1, 2)
Debug.Print matrixout(2, 0), matrixout(2, 1), matrixout(2, 2)
Debug.Print counter

End Sub



The "counter" is defined as a global integer within a module.

Upon running the Command1 subroutine, I receive an error message "constant expression required," and "counter" in the line
Public matrixout(counter, 2) As Integer

is highlighted.

This post has been edited by travis.lee: 31 July 2007 - 11:45 AM

Was This Post Helpful? 0
  • +
  • -

#4 tody4me  Icon User is offline

  • Banned
  • member icon

Reputation: 12
  • View blog
  • Posts: 1,398
  • Joined: 12-April 06

Re: How can I combine 1D arrays into a 2D array? (VB6)

Posted 31 July 2007 - 12:15 PM

Ok, first if you redim without preserve, yes you will remove anything previously put in the array, and yes, the preserve needs a constant number to increase the size.
it looks like you are just wanting to add a new row when the user calls your qvalt function. This could best be accomplished with the use of collections instead of arrays. is there a reason that you are using arrays instead?

for arrays, define an add function (such as the qvalt function you have above). The first addition will Redim Preserve the array to being a 2 dimension array, with a 0 ubound. Instead of using the counter, use Ubound(<arrayname>, 0 (the zero is required for 2d and 3d arrays, to tell which dimension to get))
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1