Sub Activity_Create(FirstTime As Boolean)
For X = 0 To 5
Log(X)
Next
For Y = 0 To 5
Dim X As Int = Y
Log(X)
Next
End Sub
NOTE: Only happens when trying to run in 'Rapid Debug' mode.
In Rapid Debug this code will fail to compile with the following error message
"An error occurred.
Error compiling program.
Current declaration does not match the previous one. Make sure to declare the variable before it is used in a For loop.
Occurred on Line: 27
Dim X As Int = Y"
In both Legacy Debug and Release mode it will compile without error and run just fine on the device.
PS. I'm pretty careful to always fully declare my variables and only fell foul of this whilst modifying someone else's code. The sample above is meaningless and purely for the purpose of recreating the error.
If I'm not mistaken, if you do not declare the variable type, B4A implies String.
The second declaration is equivalent to a Redim (VB.net) but you have changed the type to Int.
As you rightly wrote, it is better to explicitly write the type.
Hi @LucaMs I realise that declaring the type before the first For loop will cure the problem and this is how I usually code. But my concern was that the problem is only hightlighted in Rapid Debug mode. The other methods of compiling work without error.
It's also interesting that if you change the "Int" in Dim X As Int = Y to "String" or "Double" or "Object" it still throws errors. I wonder what X is initially defined as when used in the For loop without explicitly declaring the type?
If I recall correctly, the control variable, X in this case, always defaults to Int in a for statement unless you declare it differently. This means that X in most cases is Int in for statements and also in this example.
If I recall correctly, the control variable, X in this case, always defaults to Int in a for statement unless you declare it differently. This means that X in most cases is Int in for statements and also in this example.
I can agree with that as changing the type when declaring X in the second For loop throws a cast type error. Also it is quite happy to compile in Release and Legacy Debug. The problem is with Rapid Debug where it seems to be a little over zealous.
Yes, you're right. I have a motto: "Try" ; so I tried and GetType (x) returns "java.lang.Integer" (as default).
If you do not define the type of a parameter of a routine, B4A uses String. I supposed it was the same for variables (though I doubted: a loop with a String as counter? )
The issue you encounter here is indeed specific to the rapid debugger. The error message tells you how to fix it. You need to declare the variable before the first for loop and it will work.
It is difficult to explain the reason for this error without writing several pages that explain how the rapid debugger works. It is related to an optimization done by the compiler in the first loop.