Compiling problem with 'goto'

jschuchert

Active Member
Licensed User
Longtime User
Well, I'm back again. Everything was going so smoothly as I added features to my plotting routine. After the last addition (which ran fine in the IDE) I tried to compile but got the following error message:
Error CS0159...No such label within the scope of the goto statement. However, the label is there as indicated in the following code of mine. I really need to move past this so hope someone can figure out what is wrong. Thanks, guys.
B4X:
Sub frmplotpoints_mousedown(j,k)
j=upltx+(j/screenw)*(lwrtx-upltx)
j=Format(j,sdecpl)
k=uplty-(k/screenh)*(uplty-lwrty)
k=Format(k,sdecpl)

If chktraverse.Checked =True Then 
txtpointid.Visible =True
Goto traversehere  '[color=red]where the error occurs while compiling[/color]
End If

'txtpointid.Visible =True
txtpointid.Text =k & "   " & j
FileOpen(c,strfilename,cRead)
lineoftext=FileRead(c)
Do Until lineoftext=EOF
coord()=StrSplit(lineoftext,",")
If j >=coord(2)-5 AND j<=coord(2)+5 AND k>=coord(1)-5 AND k<=coord(1)+5 Then
pt=coord(0)
If intcounter=0 OR intcounter="" Then
invpoint1=pt
'areapt1=pt
End If 
If intcounter>0 Then
invpoint2=pt
areapt2=pt
End If
j=coord(2)
k=coord(1)
intcounter=intcounter+1
If chkpointid.Checked = True  Then
txtpointid.Visible =True
txtpointid.text="Pt " & coord(0) & "  N " & k & "  E " & j
Goto endthisplot
'End If
End If

If chkinverse.Checked =True Then
'If intcounter>0 Then
strbearing=invpoint1 & "*" & invpoint2
startinverse8
continueinverse
converttodms
txtpointid.Visible =True
txtpointid.Text = strNS & strdeg & Chr(176) & strmin & Chr(39) & strsec & Chr(34) _
& strEW & "  " & Format(dbldistance,sdecpl) 
invpoint1=invpoint2 'so inverses can continue between points
Goto endthisplot
End If
'End If

If chkplotarea.Checked =True Then
If areapt1=pt Then
dblnorth=coord(1)
dbleast=coord(2)
End If
If areapt2=pt Then
dblnortha=coord(1)
dbleasta=coord(2)
End If
Areasqft = Areasqft + (dblNorth * dbleasta - dblnortha * dblEast)
dblnorth=dblnortha
dbleast=dbleasta
Areasqft = Areasqft + (dblnorth * dbleasta - dblnortha * dbleast)
txtpointid.Visible =True
txtpointid.Text ="Area = " & Format(areasqft/2,sdecpl) & " sq.ft."
'End If
Goto endthisplot
End If

traversehere:   [color=red]this is the label. Error message said no such [/color]
'''If chktraverse.Checked =True Then
btntravplot.Visible =True
txtpointid.Visible =True
dblnorth=k
dbleast=j
dblnorth1=k
dbleast1=j
Goto endthisplot
'''End If  

End If     [color=red]this finishes other code above [/color]
lineoftext=FileRead(c)
Loop

endthisplot:
FileClose(c)
End Sub

I know the code is not very elegant but it works. There is a form with several checkboxes that are then activated according to this code. As I stated before, all is well when run within the IDE. I read the docs on the goto statement but really didn't understand what it was telling me about the scope equal or wider than the calling goto.

Although you can use Goto to jump between different subs it may cause unpredictable errors because the local variables will still be the variables of the previous sub.
Goto is best used to jump inside a sub.
The optimized compiler doesn't allow jumps between different subs, and the target label must be in a scope equal or wider to the calling Goto.

The jumping is done within the same sub.

Thanks for all the previous help and am hoping this is solvable.

Jim Schuchert
 

agraham

Expert
Licensed User
Longtime User
but really didn't understand what it was telling me about the scope equal or wider than the calling goto.
Simply put it means that you can jump out of or within an If/End If block or For/Next loop but can't jump into one. Studiously refraining from commenting about the use of Goto at all you might consider making the problematic block of code a separate Sub to avoid the scoping problem.
 

jschuchert

Active Member
Licensed User
Longtime User
Thank you Agraham. I can see that the label was still within a long "if-then-end if" statement so I guess that caused the compiler to throw the error. I am aware of the dangers and poor coding practice of using "go to" but after the original code was written I began adding stuff and it just evolved. I solved the problem by adding another plotting form and avoiding the issue. I wish the IDE caught those things prior to making an 'exe'. Your explanation and advice will save me heartburn in the future. Thanks again. Incidentally, will the 'auto scale' compile help with various screen resolutions and sizes?

Jim Schuchert
 

agraham

Expert
Licensed User
Longtime User
will the 'auto scale' compile help with various screen resolutions and sizes?
It should do in most cases if you also stick to using mageLib for any drawing/imaging. If the autoscaled image quality on high resolution devices is not good enough you will need to use ImageLibEx and some simple scaling of your own. If want to use the application on unusual screen sizes you may also need to perform some intervention.
 
Top