Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Problem when declaring a large # of variable with loops
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Zeppelin




PostPosted: Fri Dec 26, 2008 8:16 pm   Post subject: Problem when declaring a large # of variable with loops

For my grade 11 project my group is making a game similar to the game Advance Wars.

We decided to use a large number of image boxes to represent each tile on the map (Currently there are 225). The problem is when I started decalring the value of the Y axis for each tile I got a "runtime error 9, subscript out of range" error. I could declare the value of the X axis without problem but for some reason I always get this error when doing it for the Y axis.

The code for declaring the X and Y axis is below with the place where the error occurs marked. If it matters the declarations are made when the form loads.

code:
TileNumAlocate = 1

Do Until TileNumAlocate = 16
TilePosX(TileNumAlocate) = TileNumAlocate
TileNumAlocate = TileNumAlocate + 1
Loop

Do Until TileNumAlocate = 31
TilePosX(TileNumAlocate) = TileNumAlocate - 15
TileNumAlocate = TileNumAlocate + 1
Loop

Do Until TileNumAlocate = 46
TilePosX(TileNumAlocate) = TileNumAlocate - 30
TileNumAlocate = TileNumAlocate + 1
Loop

Do Until TileNumAlocate = 61
TilePosX(TileNumAlocate) = TileNumAlocate - 45
TileNumAlocate = TileNumAlocate + 1
Loop

Do Until TileNumAlocate = 76
TilePosX(TileNumAlocate) = TileNumAlocate - 60
TileNumAlocate = TileNumAlocate + 1
Loop

Do Until TileNumAlocate = 91
TilePosX(TileNumAlocate) = TileNumAlocate - 75
TileNumAlocate = TileNumAlocate + 1
Loop

Do Until TileNumAlocate = 106
TilePosX(TileNumAlocate) = TileNumAlocate - 90
TileNumAlocate = TileNumAlocate + 1
Loop

Do Until TileNumAlocate = 121
TilePosX(TileNumAlocate) = TileNumAlocate - 105
TileNumAlocate = TileNumAlocate + 1
Loop

Do Until TileNumAlocate = 136
TilePosX(TileNumAlocate) = TileNumAlocate - 120
TileNumAlocate = TileNumAlocate + 1
Loop

Do Until TileNumAlocate = 151
TilePosX(TileNumAlocate) = TileNumAlocate - 135
TileNumAlocate = TileNumAlocate + 1
Loop

Do Until TileNumAlocate = 166
TilePosX(TileNumAlocate) = TileNumAlocate - 150
TileNumAlocate = TileNumAlocate + 1
Loop

Do Until TileNumAlocate = 181
TilePosX(TileNumAlocate) = TileNumAlocate - 165
TileNumAlocate = TileNumAlocate + 1
Loop

Do Until TileNumAlocate = 196
TilePosX(TileNumAlocate) = TileNumAlocate - 180
TileNumAlocate = TileNumAlocate + 1
Loop

Do Until TileNumAlocate = 211
TilePosX(TileNumAlocate) = TileNumAlocate - 195
TileNumAlocate = TileNumAlocate + 1
Loop

Do Until TileNumAlocate = 226
TilePosX(TileNumAlocate) = TileNumAlocate - 210
TileNumAlocate = TileNumAlocate + 1
Loop

TileNumAlocate = 1
TileCount = 1

Do Until TileCount = 16
TilePosY(TileCount) = TileNumAlocate
TileCount = TileCount + 1
Loop

TileNumAlocate = 2

Do Until TileNumAlocate = 31
TilePosY(TileCount) = TileNumAlocate >>>Error occurs here<<<
TileCount = TileCount + 1
Loop

TileNumAlocate = 3

Do Until TileNumAlocate = 46
TilePosY(TileCount) = TileNumAlocate
TileCount = TileCount + 1
Loop

TileNumAlocate = 4

Do Until TileNumAlocate = 61
TilePosY(TileCount) = TileNumAlocate
TileCount = TileCount + 1
Loop

TileNumAlocate = 5

Do Until TileNumAlocate = 76
TilePosY(TileCount) = TileNumAlocate
TileCount = TileCount + 1
Loop

TileNumAlocate = 6

Do Until TileNumAlocate = 91
TilePosY(TileCount) = TileNumAlocate
TileCount = TileCount + 1
Loop

TileNumAlocate = 7

Do Until TileNumAlocate = 106
TilePosY(TileCount) = TileNumAlocate
TileCount = TileCount + 1
Loop

TileNumAlocate = 8

Do Until TileNumAlocate = 121
TilePosY(TileCount) = TileNumAlocate
TileCount = TileCount + 1
Loop

TileNumAlocate = 9

Do Until TileNumAlocate = 136
TilePosY(TileCount) = TileNumAlocate
TileCount = TileCount + 1
Loop

TileNumAlocate = 10

Do Until TileNumAlocate = 151
TilePosY(TileCount) = TileNumAlocate
TileCount = TileCount + 1
Loop

TileNumAlocate = 11

Do Until TileNumAlocate = 166
TilePosY(TileCount) = TileNumAlocate
TileCount = TileCount + 1
Loop

TileNumAlocate = 12

Do Until TileNumAlocate = 181
TilePosY(TileCount) = TileNumAlocate
TileCount = TileCount + 1
Loop

TileNumAlocate = 13

Do Until TileNumAlocate = 196
TilePosY(TileCount) = TileNumAlocate
TileCount = TileCount + 1
Loop

TileNumAlocate = 14

Do Until TileNumAlocate = 211
TilePosY(TileCount) = TileNumAlocate
TileCount = TileCount + 1
Loop

TileNumAlocate = 15

Do Until TileNumAlocate = 226
TilePosY(TileCount) = TileNumAlocate
TileCount = TileCount + 1
Loop


What is causing this problem?
Sponsor
Sponsor
Sponsor
sponsor
Clayton




PostPosted: Fri Dec 26, 2008 11:20 pm   Post subject: RE:Problem when declaring a large # of variable with loops

To start: Keep things DRY (Don't Repeat Yourself). There's an awful lot of similiarily repeated code there. All of those loops setting your x coordinates at the very least should be able to be set in one loop instead of 10 or more.
Zeppelin




PostPosted: Sat Dec 27, 2008 3:13 pm   Post subject: Re: Problem when declaring a large # of variable with loops

Thanks. I was able to bring down the declarations to two loops and now it works fine.
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: