Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 9th, 2005, 12:55 PM   #1
qowieur_01
Newbie
 
Join Date: Feb 2005
Posts: 4
Rep Power: 0 qowieur_01 is on a distinguished road
Unhappy arrays

i need help with my coding for some reason when i run it on VB it says "compile error: do without loop" i don't know how to run it without any errors. heres the code. For this i've got 3 txt boxes and 1 commmand box, can someone please help me fix this problem.

Dim size As Integer
Dim step As Integer
Dim row As Integer
Dim col As Integer
Dim count1 As Integer
Dim number As Integer
Dim array1(1 To 10, 1 To 10) As Integer
Dim newrow As Integer
Dim newcol As Integer
========================================
Private Sub cmdOK_Click()
Open "c:\temp\vbout01.txt" For Output As #1
Write #1,
row = 1
Write #1, "row", row
col = (size + 1) / 2
count1 = 1
Write #1, "count = ", number
number = start
Write #1, "number = ", number

Do
array1(row, col) = number
Write #1, "array"; (",row,"",col") = ",number"
number = number + step
Write #1, "number = ", number
count1 = count1 + 1
Write #1, "count =", count1
newrow = newrow - 1
Write #1, "newrow =", newrow
newcol = newcol - 1
Write #1, "newcol =", newcol

If newrow <= 1 Then
newrow = size
Write #1, "newcol =", newcol
End If

If array1(newrow, newcol) = Not Empty Then
newrow = row + 1
Write #1, "newrow=", newrow
newcol = col
Write #1, "newcol=", newcol

If newrow > size Then
newrow = 1
Write #1, "newrow=", newrow
End If

row = newrow
Write #1, "col=", col
Loop Until count1 > (size * size)
Close #1
End Sub
==============================
Private Sub sizebox_Change()
size = sizebox.Text
End Sub
=====================================
Private Sub startbox_Change()
start = startbox.Text
End Sub
=====================================
Private Sub stepbox_Change()
step = stepbox.Text
End Sub
qowieur_01 is offline   Reply With Quote
Old Feb 9th, 2005, 1:33 PM   #2
Hockeyman
Programmer
 
Hockeyman's Avatar
 
Join Date: Jan 2005
Location: Vancouver, Canada
Posts: 60
Rep Power: 4 Hockeyman is on a distinguished road
Send a message via MSN to Hockeyman
As far as I know, for a Do loop in VB you need the exit statement at the top, not at the bottom of the loop.

It should look like:

Do Until [whatever your exit condition is]
...
your code
...
Loop
__________________
~ You know, Hobbes, some days even my lucky rocketship underpants don't help. ~

Hockeyman is offline   Reply With Quote
Old Feb 9th, 2005, 2:15 PM   #3
qowieur_01
Newbie
 
Join Date: Feb 2005
Posts: 4
Rep Power: 0 qowieur_01 is on a distinguished road
i've just tried that and now when i run it it says "Compile Error: Block if without end if" and highlight "End Sub".
qowieur_01 is offline   Reply With Quote
Old Feb 9th, 2005, 2:18 PM   #4
Hockeyman
Programmer
 
Hockeyman's Avatar
 
Join Date: Jan 2005
Location: Vancouver, Canada
Posts: 60
Rep Power: 4 Hockeyman is on a distinguished road
Send a message via MSN to Hockeyman
Okay...
Looks like you're missing an "end if":

If array1(newrow, newcol) = Not Empty Then
newrow = row + 1
Write #1, "newrow=", newrow
newcol = col
Write #1, "newcol=", newcol
End IF
__________________
~ You know, Hobbes, some days even my lucky rocketship underpants don't help. ~

Hockeyman is offline   Reply With Quote
Old Feb 9th, 2005, 4:05 PM   #5
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
And BTW, you don't need to put the Until statement at the top (though it would be better if you did, in this case). You just need to get rid of the Loop keyword:
Do
     ...
 Until a = b
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Feb 10th, 2005, 6:36 AM   #6
qowieur_01
Newbie
 
Join Date: Feb 2005
Posts: 4
Rep Power: 0 qowieur_01 is on a distinguished road
i've done what you said, but now when i run it it still has an error message it says "Compile Error: Do without Loop" and highlights "end sub".
If array1(newrow, newcol) = Not Empty Then
newrow = row + 1
Write #1, "newrow=", newrow
newcol = col
Write #1, "newcol=", newcol
End If
If newrow > size Then
newrow = 1
Write #1, "newrow=", newrow
End If
row = newrow
Write #1, "col=", col
Do Until count1 > (size * size)
Close #1
End Sub
qowieur_01 is offline   Reply With Quote
Old Feb 10th, 2005, 10:10 AM   #7
Hockeyman
Programmer
 
Hockeyman's Avatar
 
Join Date: Jan 2005
Location: Vancouver, Canada
Posts: 60
Rep Power: 4 Hockeyman is on a distinguished road
Send a message via MSN to Hockeyman
okay...looks like you fixed the if statement but broke the do loop again...
You can either have a
Do Until [exit condition]
     ...code...
Loop
or
Do
     ...code...
Until [exit condition]

If you have the Do Until/While [exit condition] at the top, you have to have the Loop keyword at the end to indicate where you want it to loop to. If you just have the Do keyword at the top, you need to put the Until/While [exit condition] at the bottom of where you want it to loop, for the same reason. In your code, you've got the Do Until at the top of the loop, but nothing to indicate to the compiler where you want it to actually loop from.
__________________
~ You know, Hobbes, some days even my lucky rocketship underpants don't help. ~

Hockeyman is offline   Reply With Quote
Old Feb 12th, 2005, 12:50 PM   #8
qowieur_01
Newbie
 
Join Date: Feb 2005
Posts: 4
Rep Power: 0 qowieur_01 is on a distinguished road
i've finally fixed that do loop, but now whenever i run the program it says "Run Time Error 9: Subscripts out of range" and highlights "If array1(newrow, newcol) = 0 Then" could someone please help
qowieur_01 is offline   Reply With Quote
Old Feb 12th, 2005, 2:28 PM   #9
Hockeyman
Programmer
 
Hockeyman's Avatar
 
Join Date: Jan 2005
Location: Vancouver, Canada
Posts: 60
Rep Power: 4 Hockeyman is on a distinguished road
Send a message via MSN to Hockeyman
That means you've exceeded the bounds of array1 (tried to put a value in an array element that is higher than the upper bound of your array).
If you used: "Dim array1(n) as Integer" then you're using a static array and can't use any array element higher than n+1 (array's start counting a 0).
If you *want* to have a variable-sized array you need to declare it as a dynamic array and use "ReDim" or "ReDim Preserve" to change the size of it.
__________________
~ You know, Hobbes, some days even my lucky rocketship underpants don't help. ~

Hockeyman is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:45 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC