Home > Articles

  • Print
  • + Share This
From the author of

The code in Listing 3 shows how to use the control array. The nested For loops walk through each element in the array and position it on the form. Notice that referencing each element in the control array uses the standard "object-dot" syntax to set each property of the text box (that is, Object-dot-Property). The only exception is that you must indicate which element is being referenced by using its index. The notation therefore becomes the following:

Object(index).Property

Obviously, the same general syntax notation also applies to the object methods.

Listing 3 Using the Control Array

Private Sub ShowMatrix()
 ' Purpose: This shows the magic squares matrix.
 ' Argument List:
 '  none
 '
 ' Return value:
 '  n/a
 Dim i, j, k, offset, sum As Integer
 Dim Coor As Point

 sum = 0                  ' Calculate magic number
 k = 1
 offset = ROWOFFSET            ' Spacing between rows

 For i = 0 To MatrixSize - 1        ' rows
  For j = 0 To MatrixSize - 1       ' columns
   Coor.X = BOXWIDTH * j + SPACEBETWEEN ' Position for textbox
   Coor.Y = offset

   txtElement(k).Location = Coor     ' Set location

   txtElement(k).Text = CType(Magic(i + 1, j + 1, MatrixSize), _
              String)
   txtElement(k).Visible = True     ' Now show it
   k += 1
  Next j
  sum += CInt(txtElement(i + 1).Text)   ' Running total
  offset += BOXHEIGHT           ' Next row offset
 Next i

 txtMagicNumber.Text = CType(sum, string) ' Show magic number
 txtMagicNumber.Visible = True
 lblMagicNumber.Visible = True

End Sub

The Location property of each text box must be set using a Point object. The Point object holds the x and y coordinates for the object. Therefore, Coor determines where each text box appears on the form. The call to the Magic() function calculates the numeric value to be placed in each particular element of the text box matrix. The code for the Magic() function appears in Listing 4.

TIP

I pass the matrix size to the function, even though it's not necessary in the sample program, because of the scope of MatrixSize. That is, MatrixSize is visible everywhere within the class simply because of the scope level at which I defined MatrixSize. (Refer to Listing 1.) However, by passing MatrixSize to the function, the function is capable of standing on its own. This means that you could yank the Magic() source code from the program, throw it into a library file, and still be able to use it in other programs without using external data. It's always a good idea not to force a function to rely on external data whenever possible.

Listing 4 Determining Matrix Element Values

Private Function Magic(ByVal row As Integer, ByVal col As Integer, _
            ByVal MatrixSize As Integer) As Integer
 ' Purpose: This does the actual calculation for the magic squares
 '      matrix.
 ' Argument List:
 '  none
 '
 ' Return value:
 '  integer   the value for the specific matrix element
 Dim term1, term2 As Integer

 term1 = col - row + (MatrixSize - 1) \ 2
 term2 = col + col - row

 If term1 >= MatrixSize Then
  term1 -= MatrixSize
 ElseIf term1 < 0 Then
  term1 += MatrixSize
 End If

 If term2 > MatrixSize Then
  term2 -= MatrixSize
 ElseIf term2 <= 0 Then
  term2 += MatrixSize
 End If

 Return term1 * MatrixSize + term2

End Function

You know, I could pretend that I completely understand what the code in Listing 4 does, but I really don't. The original code comes from one of my books that was written more than 20 years ago. Given that I can't remember what I had for breakfast, I leave the understanding of the code as an exercise for the really interested reader.

  • + Share This
  • 🔖 Save To Your Account