1. Home
  2. Computing & Technology
  3. Visual Basic

Chapter 11 - Using Arrays and Collections to Manage Data

By Dan Mabbutt, About.com

3 of 6

Need to keep track of a series of values? Use an Array!

tic tac toe

tic tac toe

One of the best tutorial examples showing how array indexes can be used in VB.NET is actually an article I wrote for another reason - to describe the difficulties in using Microsoft's automatic upgrade wizard to move from VB 6 to VB.NET: The VB.NET Upgrade Wizard.

The part of the article that I'd like to highlight here is the array manipulation that is used to keep track of the "X" and "O" moves as the game is played.

~~~~~~~~~~~~~~~~~~~~~~~~~

' xo_Move contains the index value of the square clicked
' Update move counter
iMove = iMove + 1
  x = Int(xo_Move / 3) + 1
  y = (xo_Move Mod 3) + 1

' Update one of the arrays and check for a winner only in
' that array
If sPlaySign = "O" Then
  iOPos(x, y) = 1
  ' Add the O symbol to the game array and check for winner
  ' Note that the whole array is passed, not just one value
  iWin = CheckWin(iOPos)
Else
  iXPos(x, y) = 1
  ' Add the X symbol to the game array and check for winner
  ' Note that the whole array is passed, not just one value
  iWin = CheckWin(iXPos)
End If
' Change the label to the appropriate symbol
lblPlayGround(xo_Move).Text = sPlaySign
~~~~~~~~~~~~~~~~~~~~~~~~~

3 of 6

Explore Visual Basic

More from About.com

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Learn VB.NET
  5. Using Arrays and Collections to Manage Data

©2008 About.com, a part of The New York Times Company.

All rights reserved.