Possible bug in For Next

tmighty

Member
Licensed User
Longtime User
Hello.

I am getting the strange error "Missing Keyword: next" in this code, and I don't understand why.

B4X:
Sub Button1_Click
   
   Dim i As  Int 
   For i = 0 To m_lstButtons.Size-1
      If m_lstButtons.Get(i) = Sender Then
         
         If i <=(m_lstCells.Size -1) Then
         
            Dim nCell As Map 
            nCell = m_lstCells.Get(i)
            
            Dim sJump As String 
            sJump = nCell.GetValueAt (5)
            If sJump.Length >-1 Then
               Dim p As Int
               For p = 0 To m_lstPages.Size -1
                  Dim nPage As Map
                  nPage = m_lstPages.Get(p)
                  If nPage.GetValueAt(0)=sJump Then
                     tPage = nPage
                     pShow
                     Exit For
                  End If
               Next p
            End If
            
            Exit For
         Else
            Msgbox("Cell is empty",":-(")
         End If

      End If
   Next i
   
   'Just make the panel invisible to let the dashboard redraw itself
'   Dim nButton As Button
'   Dim nPanel As Panel
'   
'   nButton = Sender
'   nPanel = nButton.Tag
'   nPanel.Visible = False
   
End Sub
 

barx

Well-Known Member
Licensed User
Longtime User
This may not be your problem but it's the first thing I noticed, you do not need to do

B4X:
Next p

it's just

B4X:
Next
 

JonPM

Well-Known Member
Licensed User
Longtime User
There are many issues with this code...

Sent from my DROIDX using Tapatalk 2
 

tmighty

Member
Licensed User
Longtime User
Even when I replace "next i" and "next p" with "next", the problem persists.

I do not see anything wrong with my code. What do you mean?
 

JonPM

Well-Known Member
Licensed User
Longtime User
Even when I replace "next i" and "next p" with "next", the problem persists.

I do not see anything wrong with my code. What do you mean?


Sorry, I was looking at the post on my phone using Tapatalk and for some reason the code looks different now that I am looking at it on my laptop (unless maybe you edited the original post). Anyways, what the others have mention are the solutions to your problem.
 

rbsoft

Active Member
Licensed User
Longtime User
I prefer writing it this way to document which Next belongs to which For:
B4X:
Next'p

That's a good idea, especially for complex loops.

I don't know for B4A and the Net languages, but in VB6 it was always warned to use 'Next i'. It slowed the loop down sufficiently! Just using next was about 8 to 10 times faster.

Rolf
 

nfordbscndrd

Well-Known Member
Licensed User
Longtime User
Even when I replace "next i" and "next p" with "next", the problem persists.

I do not see anything wrong with my code. What do you mean?

A couple of things that caught my eye besides the Next p and Exit For:

B4X:
For i = 0 To m_lstButtons.Size-1
    If m_lstButtons.Get(i) = Sender Then

No code in the For-Next loop will execute unless m_lstButtons.Get(i) = Sender. So rather than the For next, wouldn't it would be more efficient to just say: i = m_lstButtons.IndexOf(Sender)?

Also, you have Dim nPage As Map inside a loop so that it is dimmed every time the loop executes. Shouldn't it be outside the loop?
 
Top