Bug? Current declaration does not match previous one.

RandomCoder

Well-Known Member
Licensed User
Longtime User
Sample code to re-create the problem...
B4X:
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.
 

LucaMs

Expert
Licensed User
Longtime User
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.
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
B4X:
Dim X As Int
    For X = 0 To 5
        Log(X)
    Next
 
    For Y = 0 To  5
        Dim X As Int = Y
        Log(X)
    Next

If you try this (added only the first line) there will be no errors.

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?
 

moster67

Expert
Licensed User
Longtime User
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.
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
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.
 

LucaMs

Expert
Licensed User
Longtime User
But my concern was that the problem is only hightlighted in Rapid Debug mode. The other methods of compiling work without error.
Yes, I'd understand; you've noticed that there is no "consistency".


If I recall correctly, the control variable, X in this case, always defaults to Int in a for statement unless you declare it differently.
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? :))
 

RandomCoder

Well-Known Member
Licensed User
Longtime User
Well I never knew there was a GetType Method...
GetType (object As Object) As String
Returns a string representing the object's java type.

Time for bed now, but at least I can go to sleep knowing that I have learnt something new today :D
 

LucaMs

Expert
Licensed User
Longtime User
Well I never knew there was a GetType Method...
GetType (object As Object) As String
Returns a string representing the object's java type.

Time for bed now, but at least I can go to sleep knowing that I have learnt something new today :D

I, too, should go to sleep; instead I'm going to write a post that will be hated and controversial :p
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The type of undeclared variables in For loops is Int.

https://www.b4x.com/android/forum/pages/results/?query=for

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.
 
Top